commands

package
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 10, 2020 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CmdCheckout = &cobra.Command{
	Use:   "checkout",
	Short: "Establece el código a la versión exacta de un Pull Request en GitHub",
	Args:  cobra.ExactArgs(1),
	RunE: func(cmd *cobra.Command, args []string) error {
		ctx := context.Background()

		id, err := strconv.ParseInt(args[0], 10, 64)
		if err != nil {
			return errors.Errorf("Especifica como primer argumento el ID del PR que quieres descargar: %v: %v", args[0], err)
		}

		branch, err := pr.Branch(ctx, id)
		if err != nil {
			return errors.Trace(err)
		}

		exists, err := query.BranchExists(branch)
		if err != nil {
			return errors.Trace(err)
		}
		if exists {
			if err := run.Git("branch", "-D", branch); err != nil {
				return errors.Trace(err)
			}
		}
		if err := run.Git("fetch", "origin", fmt.Sprintf("pull/%d/head:%s", id, branch)); err != nil {
			return errors.Trace(err)
		}

		if err := run.Git("checkout", branch); err != nil {
			return errors.Trace(err)
		}

		return nil
	},
}
View Source
var CmdCheckoutShort = &cobra.Command{
	Use:   "co",
	Short: CmdCheckout.Short,
	Args:  CmdCheckout.Args,
	RunE:  CmdCheckout.RunE,
}
View Source
var CmdLogin = &cobra.Command{
	Use:   "login",
	Short: "Inicia sesión global en GitHub para todas las operaciones relacionadas con ese tipo de repos",
	RunE: func(cmd *cobra.Command, args []string) error {
		ctx := context.Background()
		return errors.Trace(login.Start(ctx))
	},
}
View Source
var CmdPush = &cobra.Command{
	Use:   "push",
	Short: "Envía el commit a Gerrit/GitHub o crea un nuevo PR si no existe",
	RunE: func(cmd *cobra.Command, args []string) error {
		ctx := context.Background()

		gerrit, err := query.IsGerrit()
		if err != nil {
			return errors.Trace(err)
		}

		if gerrit {
			if err := run.Git("push", "origin", "HEAD:refs/for/master"); err != nil {
				return errors.Trace(err)
			}
			return nil
		}

		mainBranch, err := query.MainBranch()
		if err != nil {
			return errors.Trace(err)
		}
		branch, err := query.CurrentBranch()
		if err != nil {
			return errors.Trace(err)
		}
		if branch != mainBranch {
			branches, err := pr.ListBranches(ctx)
			if err != nil {
				return errors.Trace(err)
			}
			if collections.HasString(branches, branch) {

				if err := run.Git("push"); err != nil {
					return errors.Trace(err)
				}
				return nil
			}
		} else {
			auth, err := login.ReadAuthConfig()
			if err != nil {
				return errors.Trace(err)
			}
			if auth == nil {
				return errors.Errorf("Inicia sesión con `ci login` antes de interactuar con GitHub")
			}
			t := time.Now().In(datetime.EuropeMadrid()).Format("0405")
			branch = fmt.Sprintf("f/%s-%s", auth.Username, t)
			if err := run.Git("checkout", "-b", branch); err != nil {
				return errors.Trace(err)
			}
		}

		if err := run.Git("push", "-u", "origin", branch); err != nil {
			return errors.Trace(err)
		}

		last, err := query.LastCommitMessage()
		if err != nil {
			return errors.Trace(err)
		}

		fmt.Println()
		title, err := prompt.TextDefault("Título del PR", last)
		if err != nil {
			return errors.Trace(err)
		}
		link, err := pr.Create(ctx, title)
		if err != nil {
			return errors.Trace(err)
		}

		log.Info()
		log.Info("Se ha creado un nuevo PR en el repo de GitHub.")
		log.Info("\t" + link)
		log.Info()

		if err := run.OpenBrowser(link); err != nil {
			return errors.Trace(err)
		}

		return nil
	},
}
View Source
var CmdRoot = &cobra.Command{
	Use:           "ci",
	Short:         "Git related helper.",
	SilenceUsage:  true,
	SilenceErrors: true,
	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
		if debugApp {
			log.SetLevel(log.DebugLevel)
			log.Debug("DEBUG log level activated")
		}

		return nil
	},
}
View Source
var CmdUpdate = &cobra.Command{
	Use:   "update",
	Short: "Actualiza a la última versión de master borrando todo lo que haya en local",
	RunE: func(cmd *cobra.Command, args []string) error {
		if err := run.Git("fetch", "origin"); err != nil {
			return errors.Trace(err)
		}

		mainBranch, err := query.MainBranch()
		if err != nil {
			return errors.Trace(err)
		}

		status, err := run.GitCaptureOutput("status", "-s")
		if err != nil {
			return errors.Trace(err)
		}
		if len(status) > 0 {
			keep, err := prompt.Confirm(fmt.Sprintf("El proyecto tiene cambios. ¿Estás seguro de que deseas borrar todo y pasar a %s?", mainBranch))
			if err != nil {
				return errors.Trace(err)
			}
			if !keep {
				return nil
			}
		}

		if err := run.Git("checkout", "--", "."); err != nil {
			return errors.Trace(err)
		}
		if err := run.Git("checkout", mainBranch); err != nil {
			return errors.Trace(err)
		}
		if err := run.Git("reset", "--hard", "origin/"+mainBranch); err != nil {
			return errors.Trace(err)
		}

		status, err = run.GitCaptureOutput("status", "-s")
		if err != nil {
			return errors.Trace(err)
		}
		if len(status) > 0 {
			if err := run.Git("stash", "-q", "--include-untracked"); err != nil {
				return errors.Trace(err)
			}
			if err := run.Git("stash", "drop", "-q"); err != nil {
				return errors.Trace(err)
			}
		}

		return nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL