111 lines
2.2 KiB
Go
111 lines
2.2 KiB
Go
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",
|
|
)
|
|
}
|