move Restart and Templating to config options
and run gofmt Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
		@@ -6,8 +6,6 @@ type Source struct {
 | 
				
			|||||||
	Token       string `json:"token"`
 | 
						Token       string `json:"token"`
 | 
				
			||||||
	ConsulToken string `json:"consul_token"`
 | 
						ConsulToken string `json:"consul_token"`
 | 
				
			||||||
	VaultToken  string `json:"vault_token"`
 | 
						VaultToken  string `json:"vault_token"`
 | 
				
			||||||
	Templating  bool   `json:"templating"`
 | 
					 | 
				
			||||||
	Restart     bool   `json:"restart"`
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type JobVersion struct {
 | 
					type JobVersion struct {
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										82
									
								
								out/main.go
									
									
									
									
									
								
							
							
						
						
									
										82
									
								
								out/main.go
									
									
									
									
									
								
							@@ -16,9 +16,11 @@ import (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Params struct {
 | 
					type Params struct {
 | 
				
			||||||
	JobPath  string            `json:"job_path"`
 | 
						JobPath    string            `json:"job_path"`
 | 
				
			||||||
	Vars     map[string]string `json:"vars"`
 | 
						Vars       map[string]string `json:"vars"`
 | 
				
			||||||
	VarFiles map[string]string `json:"var_files"`
 | 
						VarFiles   map[string]string `json:"var_files"`
 | 
				
			||||||
 | 
						Templating bool              `json:"templating"`
 | 
				
			||||||
 | 
						Restart    bool              `json:"restart"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type OutConfig struct {
 | 
					type OutConfig struct {
 | 
				
			||||||
@@ -45,49 +47,45 @@ func main() {
 | 
				
			|||||||
	templPath := filepath.Join(sourceDir, config.Params.JobPath)
 | 
						templPath := filepath.Join(sourceDir, config.Params.JobPath)
 | 
				
			||||||
	templFile, err := ioutil.ReadFile(templPath)
 | 
						templFile, err := ioutil.ReadFile(templPath)
 | 
				
			||||||
	common.Check(err, "Could not read input file "+templPath)
 | 
						common.Check(err, "Could not read input file "+templPath)
 | 
				
			||||||
  if ( config.Source.Templating != false ) {
 | 
						if config.Params.Templating != false {
 | 
				
			||||||
	tmpl, err := template.New("job").Parse(string(templFile))
 | 
							tmpl, err := template.New("job").Parse(string(templFile))
 | 
				
			||||||
	common.Check(err, "Error parsing template")
 | 
							common.Check(err, "Error parsing template")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for name, path := range config.Params.VarFiles {
 | 
							for name, path := range config.Params.VarFiles {
 | 
				
			||||||
		varPath := filepath.Join(sourceDir, path)
 | 
								varPath := filepath.Join(sourceDir, path)
 | 
				
			||||||
		varFile, err := ioutil.ReadFile(varPath)
 | 
								varFile, err := ioutil.ReadFile(varPath)
 | 
				
			||||||
		common.Check(err, "Error reading var file")
 | 
								common.Check(err, "Error reading var file")
 | 
				
			||||||
		config.Params.Vars[name] = strings.TrimSpace(string(varFile))
 | 
								config.Params.Vars[name] = strings.TrimSpace(string(varFile))
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							buf := new(bytes.Buffer)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							err = tmpl.Execute(buf, config.Params.Vars)
 | 
				
			||||||
 | 
							common.Check(err, "Error executing template")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							outFile, err := os.Create(templPath)
 | 
				
			||||||
 | 
							common.Check(err, "Error creating output file")
 | 
				
			||||||
 | 
							defer outFile.Close()
 | 
				
			||||||
 | 
							_, err = outFile.Write(buf.Bytes())
 | 
				
			||||||
 | 
							common.Check(err, "Error writing output file")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	buf := new(bytes.Buffer)
 | 
						if config.Params.Restart != false {
 | 
				
			||||||
 | 
							cmd := exec.Command(
 | 
				
			||||||
	err = tmpl.Execute(buf, config.Params.Vars)
 | 
								"nomad",
 | 
				
			||||||
	common.Check(err, "Error executing template")
 | 
								"job",
 | 
				
			||||||
 | 
								"stop",
 | 
				
			||||||
	outFile, err := os.Create(templPath)
 | 
								"-purge",
 | 
				
			||||||
	common.Check(err, "Error creating output file")
 | 
								"-address="+config.Source.URL,
 | 
				
			||||||
	defer outFile.Close()
 | 
								"-token="+config.Source.Token,
 | 
				
			||||||
	_, err = outFile.Write(buf.Bytes())
 | 
								config.Source.Name,
 | 
				
			||||||
	common.Check(err, "Error writing output file")
 | 
							)
 | 
				
			||||||
 | 
							var out bytes.Buffer
 | 
				
			||||||
 | 
							cmd.Stdout = &out
 | 
				
			||||||
 | 
							cmd.Stderr = &out
 | 
				
			||||||
 | 
							_ = cmd.Run()
 | 
				
			||||||
 | 
							//Ignore even if the job fails to purge
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					 | 
				
			||||||
  if ( config.Source.Restart != false ) {
 | 
					 | 
				
			||||||
    cmd := exec.Command(
 | 
					 | 
				
			||||||
      "nomad",
 | 
					 | 
				
			||||||
      "job",
 | 
					 | 
				
			||||||
      "stop",
 | 
					 | 
				
			||||||
      "-purge",
 | 
					 | 
				
			||||||
      "-address="+config.Source.URL,
 | 
					 | 
				
			||||||
      "-token="+config.Source.Token,
 | 
					 | 
				
			||||||
      config.Source.Name,
 | 
					 | 
				
			||||||
    )
 | 
					 | 
				
			||||||
	  var out bytes.Buffer
 | 
					 | 
				
			||||||
	  cmd.Stdout = &out
 | 
					 | 
				
			||||||
	  cmd.Stderr = &out
 | 
					 | 
				
			||||||
	  err = cmd.Run()
 | 
					 | 
				
			||||||
	  if err != nil {
 | 
					 | 
				
			||||||
		  fmt.Fprintf(os.Stderr, "Error executing nomad: %s\n", err)
 | 
					 | 
				
			||||||
		  fmt.Fprint(os.Stderr, out.String())
 | 
					 | 
				
			||||||
		  os.Exit(1)
 | 
					 | 
				
			||||||
	  }
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
	cmd := exec.Command(
 | 
						cmd := exec.Command(
 | 
				
			||||||
		"nomad",
 | 
							"nomad",
 | 
				
			||||||
		"job",
 | 
							"job",
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user