Skip to content

Commit

Permalink
Merge pull request #261 from rsteube/codegen-return-error
Browse files Browse the repository at this point in the history
codegen: return error
  • Loading branch information
rsteube committed Feb 14, 2024
2 parents 9369324 + dc76ce5 commit 82e8ee3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
15 changes: 9 additions & 6 deletions codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ func (s codegenCmd) formatExecute() string {
}
`
}
func Codegen(cmd *cobra.Command) {
func Codegen(cmd *cobra.Command) error {
dir, err := os.MkdirTemp(os.TempDir(), "carapace-codegen-")
if err != nil {
panic(err.Error())
return err
}

codegen(cmd, dir)
return codegen(cmd, dir)
}

func codegen(cmd *cobra.Command, tmpDir string) {
func codegen(cmd *cobra.Command, tmpDir string) error {
out := &bytes.Buffer{}
fmt.Fprintln(out, codegenCmd{cmd}.formatHeader())
fmt.Fprintln(out, codegenCmd{cmd}.formatCommand())
Expand Down Expand Up @@ -163,16 +163,19 @@ func codegen(cmd *cobra.Command, tmpDir string) {
unformatted[line-1] = "\033[31m" + unformatted[line-1] + "\033[2;37m"
}
println("\033[2;37m" + strings.Join(unformatted, "\n") + "\033[0m")
panic(err.Error())
return err
}

os.WriteFile(filename, formatted, 0644)

for _, subcmd := range cmd.Commands() {
if subcmd.Deprecated == "" && subcmd.Name() != "_carapace" {
codegen(subcmd, tmpDir)
if err := codegen(subcmd, tmpDir); err != nil {
return err
}
}
}
return nil
}

func formatUsage(usage string) string {
Expand Down
8 changes: 4 additions & 4 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ func (c Command) ToCobraE() (*cobra.Command, error) {
return cmd, nil
}

func (c Command) Codegen() {
func (c Command) Codegen() error {
cmd, err := c.ToCobraE()
// TODO handle error
if err == nil {
Codegen(cmd)
if err != nil {
return err
}
return Codegen(cmd)
}

func (c Command) addPersistentFlags(cmd *cobra.Command) error {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/invopop/jsonschema v0.12.0
github.com/rsteube/carapace v0.49.1
github.com/rsteube/carapace v0.49.2
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
gopkg.in/yaml.v3 v3.0.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFF
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/rsteube/carapace v0.49.1 h1:ljGQpEkSATpvAnP6eO7Bc1iLJljxgfpCJoCPA/EkQlU=
github.com/rsteube/carapace v0.49.1/go.mod h1:syVOvI8e2rEEK/9aMZxfWuHvcnQK/EcnTV4roClEnLE=
github.com/rsteube/carapace v0.49.2 h1:BMtZK/iGeGFF2cAIn+x0zM46ZbNt7OM+p18sHHXpXm4=
github.com/rsteube/carapace v0.49.2/go.mod h1:syVOvI8e2rEEK/9aMZxfWuHvcnQK/EcnTV4roClEnLE=
github.com/rsteube/carapace-shlex v0.1.2 h1:ZKjhIfXoCkEnzensMglTaLbkNOaLkmM8SCRshpJKx6s=
github.com/rsteube/carapace-shlex v0.1.2/go.mod h1:zPw1dOFwvLPKStUy9g2BYKanI6bsQMATzDMYQQybo3o=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down

0 comments on commit 82e8ee3

Please sign in to comment.