diff --git a/cmd/ninja/cmd/plugin.go b/cmd/ninja/cmd/plugin.go index cb39b7c..c3437b3 100644 --- a/cmd/ninja/cmd/plugin.go +++ b/cmd/ninja/cmd/plugin.go @@ -76,29 +76,25 @@ func newPluginInitCmd() *cobra.Command { } } - resp, err := cli.Reg.CreatePlugin(ctx, connect.NewRequest(&v1.CreatePluginRequest{ + if _, err := cli.Reg.CreatePlugin(ctx, connect.NewRequest(&v1.CreatePluginRequest{ ScopeSlug: scopeAPISlug(scope), Name: name, Description: "", - })) - if err != nil { + })); err != nil { return err } fmt.Printf("\nCreated %s/%s\n", scope, name) - fmt.Printf(" Git remote: %s\n", resp.Msg.GitRemoteUrl) - - if _, err := os.Stat(".git"); err == nil { - _ = runCmd("git", "remote", "remove", "ninja") - if err := runCmd("git", "remote", "add", "ninja", resp.Msg.GitRemoteUrl); err != nil { - return fmt.Errorf("git remote add: %w", err) - } - fmt.Println("Added git remote 'ninja'") - } else { - fmt.Println("Not in a git repo - skipped adding remote") - } if err := upsertPluginMod(scope, name); err != nil { return err } fmt.Println("plugin.mod updated") + + if _, err := os.Stat(".git"); err == nil { + if err := autoCommitPluginMod(); err != nil { + fmt.Fprintf(os.Stderr, "warning: could not commit plugin.mod: %v\n", err) + } + } else { + fmt.Println("Not in a git repo - run `git init` before `ninja plugin publish`") + } return nil }, } @@ -509,3 +505,23 @@ func writeMod(path string, m *core.ModFile) error { } return os.WriteFile(path, []byte(b.String()), 0o644) } + +// autoCommitPluginMod stages and commits plugin.mod if it differs from +// what's already at HEAD. No-op when there's nothing to commit. +func autoCommitPluginMod() error { + out, err := exec.Command("git", "status", "--porcelain", "plugin.mod").Output() + if err != nil { + return fmt.Errorf("git status: %w", err) + } + if strings.TrimSpace(string(out)) == "" { + return nil + } + if err := runCmd("git", "add", "plugin.mod"); err != nil { + return err + } + if err := runCmd("git", "commit", "-m", "Add plugin.mod"); err != nil { + return err + } + fmt.Println("Committed plugin.mod") + return nil +}