feat(cli): init auto-commits plugin.mod, drops ninja git remote

This commit is contained in:
Alex Dunmow 2026-06-03 01:34:42 +08:00
parent e5b27f5a65
commit c825942c8d

View File

@ -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: "", ScopeSlug: scopeAPISlug(scope), Name: name, Description: "",
})) })); err != nil {
if err != nil {
return err return err
} }
fmt.Printf("\nCreated %s/%s\n", scope, name) 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 { if err := upsertPluginMod(scope, name); err != nil {
return err return err
} }
fmt.Println("plugin.mod updated") 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 return nil
}, },
} }
@ -509,3 +505,23 @@ func writeMod(path string, m *core.ModFile) error {
} }
return os.WriteFile(path, []byte(b.String()), 0o644) 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
}