|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "github.com/uselagoon/lagoon-cli/pkg/output" |
| 11 | + lclient "github.com/uselagoon/machinery/api/lagoon/client" |
| 12 | + "gopkg.in/yaml.v3" |
| 13 | +) |
| 14 | + |
| 15 | +// CustomCommand is the custom command data structure, this is what can be used to define custom commands |
| 16 | +type CustomCommand struct { |
| 17 | + Name string `yaml:"name"` |
| 18 | + Description string `yaml:"description"` |
| 19 | + Query string `yaml:"query"` |
| 20 | + Flags []struct { |
| 21 | + Name string `yaml:"name"` |
| 22 | + Description string `yaml:"description"` |
| 23 | + Variable string `yaml:"variable"` |
| 24 | + Type string `yaml:"type"` |
| 25 | + Required bool `yaml:"required"` |
| 26 | + Default *interface{} `yaml:"default,omitempty"` |
| 27 | + } `yaml:"flags"` |
| 28 | +} |
| 29 | + |
| 30 | +var emptyCmd = cobra.Command{ |
| 31 | + Use: "none", |
| 32 | + Aliases: []string{""}, |
| 33 | + Short: "none", |
| 34 | + Hidden: true, |
| 35 | + PreRunE: func(_ *cobra.Command, _ []string) error { |
| 36 | + return validateTokenE(lagoonCLIConfig.Current) |
| 37 | + }, |
| 38 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 39 | + return nil |
| 40 | + }, |
| 41 | +} |
| 42 | + |
| 43 | +var rawCmd = &cobra.Command{ |
| 44 | + Use: "raw", |
| 45 | + Aliases: []string{"r"}, |
| 46 | + Short: "Run a custom query or mutation", |
| 47 | + Long: `Run a custom query or mutation. |
| 48 | +The output of this command will be the JSON response from the API`, |
| 49 | + PreRunE: func(_ *cobra.Command, _ []string) error { |
| 50 | + return validateTokenE(cmdLagoon) |
| 51 | + }, |
| 52 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 53 | + debug, err := cmd.Flags().GetBool("debug") |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + raw, err := cmd.Flags().GetString("raw") |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + if err := requiredInputCheck("Raw query or mutation", raw); err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + current := lagoonCLIConfig.Current |
| 65 | + token := lagoonCLIConfig.Lagoons[current].Token |
| 66 | + lc := lclient.New( |
| 67 | + lagoonCLIConfig.Lagoons[current].GraphQL, |
| 68 | + lagoonCLIVersion, |
| 69 | + &token, |
| 70 | + debug) |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + rawResp, err := lc.ProcessRaw(context.TODO(), raw, nil) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + r, err := json.Marshal(rawResp) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + fmt.Println(string(r)) |
| 83 | + return nil |
| 84 | + }, |
| 85 | +} |
| 86 | + |
| 87 | +var customCmd = &cobra.Command{ |
| 88 | + Use: "custom", |
| 89 | + Aliases: []string{"cus", "cust"}, |
| 90 | + Short: "Run a custom command", |
| 91 | + Long: `Run a custom command. |
| 92 | +This command alone does nothing, but you can create custom commands and put them into the custom commands directory, |
| 93 | +these commands will then be available to use. |
| 94 | +The directory for custom commands is ${HOME}/.lagoon-cli/commands.`, |
| 95 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 96 | + // just return the help menu for this command as if it is just a normal parent with children commands |
| 97 | + cmd.Help() |
| 98 | + return nil |
| 99 | + }, |
| 100 | +} |
| 101 | + |
| 102 | +func ReadCustomCommands() ([]*cobra.Command, error) { |
| 103 | + userPath, err := os.UserHomeDir() |
| 104 | + if err != nil { |
| 105 | + return nil, fmt.Errorf("couldn't get $HOME: %v", err) |
| 106 | + } |
| 107 | + customCommandsFilePath := fmt.Sprintf("%s/%s", userPath, commandsFilePath) |
| 108 | + if _, err := os.Stat(customCommandsFilePath); os.IsNotExist(err) { |
| 109 | + err := os.MkdirAll(customCommandsFilePath, 0700) |
| 110 | + if err != nil { |
| 111 | + return nil, fmt.Errorf("couldn't create command directory %s: %v", customCommandsFilePath, err) |
| 112 | + } |
| 113 | + } |
| 114 | + files, err := os.ReadDir(customCommandsFilePath) |
| 115 | + if err != nil { |
| 116 | + return nil, fmt.Errorf("couldn't open command directory %s: %v", customCommandsFilePath, err) |
| 117 | + } |
| 118 | + var cmds []*cobra.Command |
| 119 | + if len(files) != 0 { |
| 120 | + for _, file := range files { |
| 121 | + if !file.IsDir() { |
| 122 | + data, err := os.ReadFile(customCommandsFilePath + "/" + file.Name()) |
| 123 | + if err != nil { |
| 124 | + return nil, err |
| 125 | + } |
| 126 | + raw := CustomCommand{} |
| 127 | + err = yaml.Unmarshal(data, &raw) |
| 128 | + if err != nil { |
| 129 | + return nil, fmt.Errorf("unable to unmarshal custom command '%s', yaml is likely invalid: %v", file.Name(), err) |
| 130 | + } |
| 131 | + cCmd := cobra.Command{ |
| 132 | + Use: raw.Name, |
| 133 | + Aliases: []string{""}, |
| 134 | + Short: raw.Description, |
| 135 | + PreRunE: func(_ *cobra.Command, _ []string) error { |
| 136 | + return validateTokenE(lagoonCLIConfig.Current) |
| 137 | + }, |
| 138 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 139 | + debug, err := cmd.Flags().GetBool("debug") |
| 140 | + if err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + |
| 144 | + variables := make(map[string]interface{}) |
| 145 | + var value interface{} |
| 146 | + // handling reading the custom flags |
| 147 | + for _, flag := range raw.Flags { |
| 148 | + switch flag.Type { |
| 149 | + case "Int": |
| 150 | + value, err = cmd.Flags().GetInt(flag.Name) |
| 151 | + if err != nil { |
| 152 | + return err |
| 153 | + } |
| 154 | + if flag.Required { |
| 155 | + if err := requiredInputCheck(flag.Name, fmt.Sprintf("%d", value.(int))); err != nil { |
| 156 | + return err |
| 157 | + } |
| 158 | + } |
| 159 | + case "String": |
| 160 | + value, err = cmd.Flags().GetString(flag.Name) |
| 161 | + if err != nil { |
| 162 | + return err |
| 163 | + } |
| 164 | + if flag.Required { |
| 165 | + if err := requiredInputCheck(flag.Name, value.(string)); err != nil { |
| 166 | + return err |
| 167 | + } |
| 168 | + } |
| 169 | + case "Boolean": |
| 170 | + value, err = cmd.Flags().GetBool(flag.Name) |
| 171 | + if err != nil { |
| 172 | + return err |
| 173 | + } |
| 174 | + } |
| 175 | + variables[flag.Variable] = value |
| 176 | + } |
| 177 | + |
| 178 | + current := lagoonCLIConfig.Current |
| 179 | + token := lagoonCLIConfig.Lagoons[current].Token |
| 180 | + lc := lclient.New( |
| 181 | + lagoonCLIConfig.Lagoons[current].GraphQL, |
| 182 | + lagoonCLIVersion, |
| 183 | + &token, |
| 184 | + debug) |
| 185 | + if err != nil { |
| 186 | + return err |
| 187 | + } |
| 188 | + |
| 189 | + rawResp, err := lc.ProcessRaw(context.TODO(), raw.Query, variables) |
| 190 | + if err != nil { |
| 191 | + return err |
| 192 | + } |
| 193 | + r, err := json.Marshal(rawResp) |
| 194 | + if err != nil { |
| 195 | + return err |
| 196 | + } |
| 197 | + fmt.Println(string(r)) |
| 198 | + return nil |
| 199 | + }, |
| 200 | + } |
| 201 | + // add custom flags to the command |
| 202 | + for _, flag := range raw.Flags { |
| 203 | + switch flag.Type { |
| 204 | + case "Int": |
| 205 | + if flag.Default != nil { |
| 206 | + cCmd.Flags().Int(flag.Name, (*flag.Default).(int), flag.Description) |
| 207 | + } else { |
| 208 | + cCmd.Flags().Int(flag.Name, 0, flag.Description) |
| 209 | + } |
| 210 | + case "String": |
| 211 | + if flag.Default != nil { |
| 212 | + cCmd.Flags().String(flag.Name, (*flag.Default).(string), flag.Description) |
| 213 | + } else { |
| 214 | + cCmd.Flags().String(flag.Name, "", flag.Description) |
| 215 | + } |
| 216 | + case "Boolean": |
| 217 | + if flag.Default != nil { |
| 218 | + cCmd.Flags().Bool(flag.Name, (*flag.Default).(bool), flag.Description) |
| 219 | + } else { |
| 220 | + cCmd.Flags().Bool(flag.Name, false, flag.Description) |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + cmds = append(cmds, &cCmd) |
| 225 | + } |
| 226 | + } |
| 227 | + } else { |
| 228 | + cmds = append(cmds, |
| 229 | + // create a hidden command that does nothing so help and docs can be generated for the custom command |
| 230 | + &emptyCmd) |
| 231 | + } |
| 232 | + return cmds, nil |
| 233 | +} |
| 234 | + |
| 235 | +func init() { |
| 236 | + if _, ok := os.LookupEnv("LAGOON_GEN_DOCS"); ok { |
| 237 | + // this is an override for when the docs are generated |
| 238 | + // so that it doesn't include any custom commands |
| 239 | + customCmd.AddCommand(&emptyCmd) |
| 240 | + } else { |
| 241 | + // read any custom commands |
| 242 | + cmds, err := ReadCustomCommands() |
| 243 | + if err != nil { |
| 244 | + output.RenderError(err.Error(), outputOptions) |
| 245 | + os.Exit(1) |
| 246 | + } |
| 247 | + for _, c := range cmds { |
| 248 | + customCmd.AddCommand(c) |
| 249 | + } |
| 250 | + } |
| 251 | + rawCmd.Flags().String("raw", "", "The raw query or mutation to run") |
| 252 | +} |
0 commit comments