-
Notifications
You must be signed in to change notification settings - Fork 2k
/
job_init.go
126 lines (104 loc) · 3.03 KB
/
job_init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package command
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/posener/complete"
)
const (
// DefaultInitName is the default name we use when
// initializing the example file
DefaultInitName = "example.nomad"
)
// JobInitCommand generates a new job template that you can customize to your
// liking, like vagrant init
type JobInitCommand struct {
Meta
}
func (c *JobInitCommand) Help() string {
helpText := `
Usage: nomad job init <filename>
Alias: nomad init <filename>
Creates an example job file that can be used as a starting point to customize
further. If no filename is given, the default of "example.nomad" will be used.
Init Options:
-short
If the short flag is set, a minimal jobspec without comments is emitted.
-connect
If the connect flag is set, the jobspec includes Consul Connect integration.
`
return strings.TrimSpace(helpText)
}
func (c *JobInitCommand) Synopsis() string {
return "Create an example job file"
}
func (c *JobInitCommand) AutocompleteFlags() complete.Flags {
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
complete.Flags{
"-short": complete.PredictNothing,
})
}
func (c *JobInitCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictNothing
}
func (c *JobInitCommand) Name() string { return "job init" }
func (c *JobInitCommand) Run(args []string) int {
var short bool
var connect bool
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&short, "short", false, "")
flags.BoolVar(&connect, "connect", false, "")
if err := flags.Parse(args); err != nil {
return 1
}
// Check for misuse
// Check that we either got no filename or exactly one.
args = flags.Args()
if len(args) > 1 {
c.Ui.Error("This command takes either no arguments or one: <filename>")
c.Ui.Error(commandErrorText(c))
return 1
}
filename := DefaultInitName
if len(args) == 1 {
filename = args[0]
}
// Check if the file already exists
_, err := os.Stat(filename)
if err != nil && !os.IsNotExist(err) {
c.Ui.Error(fmt.Sprintf("Failed to stat '%s': %v", filename, err))
return 1
}
if !os.IsNotExist(err) {
c.Ui.Error(fmt.Sprintf("Job '%s' already exists", filename))
return 1
}
var jobSpec []byte
switch {
case connect && !short:
jobSpec, err = Asset("command/assets/connect.nomad")
case connect && short:
jobSpec, err = Asset("command/assets/connect-short.nomad")
case !connect && short:
jobSpec, err = Asset("command/assets/example-short.nomad")
default:
jobSpec, err = Asset("command/assets/example.nomad")
}
if err != nil {
// should never see this because we've precompiled the assets
// as part of `make generate-examples`
c.Ui.Error(fmt.Sprintf("Accessed non-existent asset: %s", err))
return 1
}
// Write out the example
err = ioutil.WriteFile(filename, jobSpec, 0660)
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to write '%s': %v", filename, err))
return 1
}
// Success
c.Ui.Output(fmt.Sprintf("Example job file written to %s", filename))
return 0
}