WIP: operator collaboration MVP

See toolshed/organising#467
This commit is contained in:
2024-12-30 00:46:25 +01:00
parent 74108b0dd9
commit 4d7c812fe2
13 changed files with 319 additions and 20 deletions

51
cli/app/diff.go Normal file
View File

@ -0,0 +1,51 @@
package app
import (
"fmt"
"path/filepath"
"coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/pkg/autocomplete"
gitPkg "coopcloud.tech/abra/pkg/git"
"coopcloud.tech/abra/pkg/log"
"github.com/spf13/cobra"
)
var AppDiffCommand = &cobra.Command{
Use: "diff <app> [flags]",
Aliases: []string{"df"},
Short: "Show diff of app env changes",
Long: `This command requires /usr/bin/git.`,
Example: " abra app diff 1312.net",
Args: cobra.ExactArgs(1),
ValidArgsFunction: func(
cmd *cobra.Command,
args []string,
toComplete string) ([]string, cobra.ShellCompDirective) {
return autocomplete.AppNameComplete()
},
Run: func(cmd *cobra.Command, args []string) {
app := internal.ValidateApp(args)
gitDir := gitPkg.FindDir(app.Path)
if gitDir == "" {
log.Fatal(fmt.Errorf("no git repo found for %s", app.Name))
}
fpath := app.Path
realPath, err := filepath.EvalSymlinks(fpath)
if err != nil {
log.Fatalf("unable to app env: broken symlink: %s", fpath)
}
fpath = realPath
diff, err := gitPkg.DiffUnstaged(gitDir, fpath)
if err != nil {
log.Fatalf("unable to diff %s: %s", app.Name, err)
}
if diff != "" {
fmt.Print(diff)
}
},
}

110
cli/app/push.go Normal file
View File

@ -0,0 +1,110 @@
package app
import (
"fmt"
"path/filepath"
"coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/pkg/autocomplete"
gitPkg "coopcloud.tech/abra/pkg/git"
"coopcloud.tech/abra/pkg/log"
"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"
)
var AppPushCommand = &cobra.Command{
Use: "push <app> [flags]",
Aliases: []string{"pu"},
Short: "Push app changes to a remote",
Long: `Run "abra app pull <app>" beforehand to reduce conflicts.`,
Example: "abra app push 1312.net",
Args: cobra.ExactArgs(1),
ValidArgsFunction: func(
cmd *cobra.Command,
args []string,
toComplete string) ([]string, cobra.ShellCompDirective) {
return autocomplete.AppNameComplete()
},
Run: func(cmd *cobra.Command, args []string) {
app := internal.ValidateApp(args)
gitDir := gitPkg.FindDir(app.Path)
if gitDir == "" {
log.Fatal(fmt.Errorf("no git repo found for %s", app.Name))
}
appDir := filepath.Dir(app.Path)
log.Infof("%s currently has these unstaged changes 👇", app.Name)
diff, err := gitPkg.DiffUnstaged(appDir, app.Path)
if err != nil {
log.Fatal(err)
}
if diff == "" {
log.Infof("no diff for %s, nothing to push", app.Name)
return
}
fmt.Print(diff)
var confirmPush bool
if !internal.NoInput {
prompt := &survey.Confirm{
Message: "push these changes?",
}
if err := survey.AskOne(prompt, &confirmPush); err != nil {
log.Fatal(err)
}
}
if msg == "" && !internal.NoInput {
prompt := &survey.Input{
Message: "commit message?",
}
if err := survey.AskOne(prompt, &msg); err != nil {
log.Fatal(err)
}
}
if msg == "" {
log.Fatal("missing --msg/-m")
}
if confirmPush || internal.NoInput {
fname := filepath.Base(app.Path)
if err := gitPkg.CommitFile(gitDir, fname, msg, internal.Dry); err != nil {
log.Fatal(err)
}
if err := gitPkg.Push(gitDir, "origin", false, internal.Dry); err != nil {
log.Fatal(err)
}
log.Info("changes pushed successfully 🦋")
}
},
}
var (
msg string
)
func init() {
AppPushCommand.Flags().StringVarP(
&msg,
"msg",
"m",
"",
"commit message",
)
AppPushCommand.Flags().BoolVarP(
&internal.Dry,
"dry-run",
"r",
false,
"report changes that would be made",
)
}

View File

@ -1,6 +1,8 @@
package recipe
import (
"fmt"
"coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/pkg/autocomplete"
gitPkg "coopcloud.tech/abra/pkg/git"
@ -22,8 +24,12 @@ var RecipeDiffCommand = &cobra.Command{
},
Run: func(cmd *cobra.Command, args []string) {
r := internal.ValidateRecipe(args, cmd.Name())
if err := gitPkg.DiffUnstaged(r.Dir); err != nil {
diff, err := gitPkg.DiffUnstaged(r.Dir, "")
if err != nil {
log.Fatal(err)
}
if diff != "" {
fmt.Print(diff)
}
},
}

View File

@ -118,9 +118,13 @@ your SSH keys configured on your account.`,
if !isClean {
log.Infof("%s currently has these unstaged changes 👇", recipe.Name)
if err := gitPkg.DiffUnstaged(recipe.Dir); err != nil {
diff, err := gitPkg.DiffUnstaged(recipe.Dir, "")
if err != nil {
log.Fatal(err)
}
if diff != "" {
fmt.Print(diff)
}
}
if len(tags) > 0 {

View File

@ -208,9 +208,13 @@ likely to change.
}
if !isClean {
log.Infof("%s currently has these unstaged changes 👇", recipe.Name)
if err := gitPkg.DiffUnstaged(recipe.Dir); err != nil {
diff, err := gitPkg.DiffUnstaged(recipe.Dir, "")
if err != nil {
log.Fatal(err)
}
if diff != "" {
fmt.Print(diff)
}
}
},
}

View File

@ -327,9 +327,13 @@ You may invoke this command in "wizard" mode and be prompted for input.`,
}
if !isClean {
log.Infof("%s currently has these unstaged changes 👇", recipe.Name)
if err := gitPkg.DiffUnstaged(recipe.Dir); err != nil {
diff, err := gitPkg.DiffUnstaged(recipe.Dir, "")
if err != nil {
log.Fatal(err)
}
if diff != "" {
fmt.Print(diff)
}
}
},
}

View File

@ -187,9 +187,11 @@ func Run(version, commit string) {
app.AppUndeployCommand,
app.AppUpgradeCommand,
app.AppVolumeCommand,
app.AppDiffCommand,
app.AppPushCommand,
)
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
os.Exit(1)
}
}