|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "github.com/BurntSushi/toml" |
| 10 | + "github.com/jclem/konk/konk/debugger" |
| 11 | + "github.com/jclem/konk/konk/konkfile" |
| 12 | + "github.com/spf13/cobra" |
| 13 | + "gopkg.in/yaml.v3" |
| 14 | +) |
| 15 | + |
| 16 | +var konkfilePath string |
| 17 | + |
| 18 | +var execCommand = cobra.Command{ |
| 19 | + Use: "exec <command>", |
| 20 | + Aliases: []string{"e"}, |
| 21 | + Short: "Execute a command from a konkfile (alias: e)", |
| 22 | + Args: cobra.ExactArgs(1), |
| 23 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 24 | + dbg := debugger.Get(cmd.Context()) |
| 25 | + dbg.Flags(cmd) |
| 26 | + |
| 27 | + if workingDirectory != "" { |
| 28 | + if err := os.Chdir(workingDirectory); err != nil { |
| 29 | + return fmt.Errorf("changing working directory: %w", err) |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + kfsearch := []string{"konkfile", "konkfile.json", "konkfile.toml", "konkfile.yaml", "konkfile.yml"} |
| 34 | + if konkfilePath != "" { |
| 35 | + kfsearch = []string{konkfilePath} |
| 36 | + } |
| 37 | + |
| 38 | + var kf []byte |
| 39 | + var kfpath string |
| 40 | + |
| 41 | + for _, kfp := range kfsearch { |
| 42 | + b, err := os.ReadFile(kfp) |
| 43 | + if err != nil { |
| 44 | + if os.IsNotExist(err) { |
| 45 | + continue |
| 46 | + } |
| 47 | + |
| 48 | + return fmt.Errorf("reading konkfile: %w", err) |
| 49 | + } |
| 50 | + |
| 51 | + kf = b |
| 52 | + kfpath = kfp |
| 53 | + } |
| 54 | + |
| 55 | + ext := filepath.Ext(kfpath) |
| 56 | + var file konkfile.File |
| 57 | + |
| 58 | + if ext == "" { |
| 59 | + if err := json.Unmarshal(kf, &file); err != nil { |
| 60 | + if err := yaml.Unmarshal(kf, &file); err != nil { |
| 61 | + if err := toml.Unmarshal(kf, &file); err != nil { |
| 62 | + return fmt.Errorf("unmarshalling konkfile: %w", err) |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } else if ext == ".yaml" || ext == ".yml" { |
| 67 | + if err := yaml.Unmarshal(kf, &file); err != nil { |
| 68 | + return fmt.Errorf("unmarshalling konkfile: %w", err) |
| 69 | + } |
| 70 | + } else if ext == ".toml" { |
| 71 | + if err := toml.Unmarshal(kf, &file); err != nil { |
| 72 | + return fmt.Errorf("unmarshalling konkfile: %w", err) |
| 73 | + } |
| 74 | + } else { |
| 75 | + if err := json.Unmarshal(kf, &file); err != nil { |
| 76 | + return fmt.Errorf("unmarshalling konkfile: %w", err) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if err := konkfile.Execute(cmd.Context(), file, args[0], konkfile.ExecuteConfig{ |
| 81 | + AggregateOutput: aggregateOutput, |
| 82 | + ContinueOnError: continueOnError, |
| 83 | + NoColor: noColor, |
| 84 | + NoShell: noShell, |
| 85 | + }); err != nil { |
| 86 | + return fmt.Errorf("executing command: %w", err) |
| 87 | + } |
| 88 | + |
| 89 | + return nil |
| 90 | + }, |
| 91 | +} |
| 92 | + |
| 93 | +func init() { |
| 94 | + execCommand.Flags().StringVarP(&workingDirectory, "working-directory", "w", "", "set the working directory for all commands") |
| 95 | + execCommand.Flags().BoolVarP(&aggregateOutput, "aggregate-output", "g", false, "aggregate command output") |
| 96 | + execCommand.Flags().BoolVarP(&continueOnError, "continue-on-error", "c", false, "continue running commands after a failure") |
| 97 | + execCommand.Flags().StringVarP(&konkfilePath, "konkfile", "k", "", "path to konkfile") |
| 98 | + rootCmd.AddCommand(&execCommand) |
| 99 | +} |
0 commit comments