-
Notifications
You must be signed in to change notification settings - Fork 18
/
project.go
93 lines (84 loc) · 2.73 KB
/
project.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
package projectcmd
import (
"os"
"github.com/spf13/cobra"
"github.com/ubclaunchpad/inertia/cfg"
"github.com/ubclaunchpad/inertia/cmd/core"
"github.com/ubclaunchpad/inertia/cmd/core/utils/input"
"github.com/ubclaunchpad/inertia/cmd/core/utils/out"
"github.com/ubclaunchpad/inertia/local"
)
// ProjectCmd is the parent class for the 'config' subcommands
type ProjectCmd struct {
*cobra.Command
config *cfg.Project
projectConfigPath string
}
// AttachProjectCmd attaches the 'config' subcommands to the given parent
func AttachProjectCmd(inertia *core.Cmd) {
var project = &ProjectCmd{
projectConfigPath: inertia.ProjectConfigPath,
}
project.Command = &cobra.Command{
Use: "project [command]",
Short: "Update and configure Inertia project settings",
Long: `Update and configure Inertia settings pertaining to this project.
To create a new project, use 'inertia init'.
For configuring remote settings, use 'inertia remote'.`,
PersistentPreRun: func(*cobra.Command, []string) {
var err error
project.config, err = local.GetProject(project.projectConfigPath)
if err != nil {
out.Printf("could not find project configuration at '%s': %s",
project.projectConfigPath, err.Error())
out.Fatal("try instantiating a new project using 'inertia init'")
}
},
}
AttachProfileCmd(project)
project.attachSetCmd()
project.attachResetCmd()
inertia.AddCommand(project.Command)
}
func (root *ProjectCmd) attachSetCmd() {
var set = &cobra.Command{
Use: "set [property] [value]",
Short: "Update a property of your Inertia project configuration",
Long: `Updates a property of your Inertia project configuration and save it to inertia.toml.`,
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
if err := cfg.SetProperty(args[0], args[1], root.config); err != nil {
if err := local.Write(root.projectConfigPath, root.config); err != nil {
out.Fatal(err)
}
out.Println("configuration setting '" + args[0] + "' has been updated")
} else {
out.Println("configuration setting '" + args[0] + "' not found")
}
},
}
root.AddCommand(set)
}
func (root *ProjectCmd) attachResetCmd() {
var reset = &cobra.Command{
Use: "reset",
Short: "Remove project configuration",
Long: `Removes your project configuration by deleting the configuration file.
This is irreversible.`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
resp, err := input.Prompt("would you like to reset your project configuration?")
if err != nil {
out.Fatal(err)
}
if resp == "y" || resp == "yes" {
if err := os.Remove(root.projectConfigPath); err != nil {
out.Fatal(err)
}
} else {
out.Fatal("aborting")
}
},
}
root.AddCommand(reset)
}