Skip to content

Commit

Permalink
Merge pull request #7 from kitagry/add-new-flag
Browse files Browse the repository at this point in the history
Add option to save Job
  • Loading branch information
kitagry authored Oct 21, 2021
2 parents 6837cb1 + 4adef05 commit f9297c6
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func run() int {
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
filename := flag.String("f", "", "(optional) filename to save Job resource")
flag.Usage = func() {
fmt.Printf(`%[1]s - create custom job from cronjob template
Expand Down Expand Up @@ -82,7 +83,7 @@ Options:
return exitStatusErr
}

if err = createJob(job); err != nil {
if err = createJobWithFileName(filename, job); err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", cmdName, err)
return exitStatusErr
}
Expand Down Expand Up @@ -193,13 +194,26 @@ func randStr(n int) (string, error) {
return builder.String(), nil
}

func createJob(job *batchv1.Job) error {
f, err := os.CreateTemp("", "kj.*.yaml")
if err != nil {
return err
func createJobWithFileName(filename *string, job *batchv1.Job) error {
var f *os.File
var err error
if filename == nil || *filename == "" {
f, err = os.CreateTemp("", "kj.*.yaml")
if err != nil {
return err
}
defer os.Remove(f.Name())
} else {
f, err = os.Create(*filename)
if err != nil {
return err
}
}
defer os.Remove(f.Name())

return createJob(f, job)
}

func createJob(f *os.File, job *batchv1.Job) error {
data, err := yaml.Marshal(job)
if err != nil {
return err
Expand Down

0 comments on commit f9297c6

Please sign in to comment.