| 1 | // Copyright (c) Bartłomiej Płotka @bwplotka |
| 2 | // Licensed under the Apache License 2.0. |
| 3 | |
| 4 | package main |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "fmt" |
| 9 | "log" |
| 10 | "os" |
| 11 | "os/signal" |
| 12 | "path/filepath" |
| 13 | "regexp" |
| 14 | "syscall" |
| 15 | |
| 16 | "github.com/pkg/errors" |
| 17 | "github.com/spf13/cobra" |
| 18 | |
| 19 | "github.com/bwplotka/bingo/pkg/bingo" |
| 20 | "github.com/bwplotka/bingo/pkg/runner" |
| 21 | "github.com/bwplotka/bingo/pkg/version" |
| 22 | ) |
| 23 | |
| 24 | func NewBingoGetCommand(logger *log.Logger) *cobra.Command { |
| 25 | var ( |
| 26 | goCmd string |
| 27 | rename string |
| 28 | name string |
| 29 | insecure bool |
| 30 | link bool |
| 31 | timeOut uint |
| 32 | ) |
| 33 | |
| 34 | cmd := &cobra.Command{ |
| 35 | Use: "get [flags] [<package or binary>[@version1 or none,version2,version3...]]", |
| 36 | Example: "bingo get github.com/fatih/faillint\n" + |
| 37 | "bingo get github.com/fatih/faillint@latest\n" + |
| 38 | "bingo get github.com/fatih/[email protected]\n" + |
| 39 | "bingo get github.com/fatih/[email protected],v1.5.0\n" + |
| 40 | "bingo get github.com/fatih/faillint@none // this will be deleted ", |
| 41 | Short: "add development tools to the current project (e.g: bingo get github.com/fatih/faillint@latest)", |
| 42 | Long: "go get like, simple CLI that allows automated versioning of Go package level \n" + |
| 43 | "binaries(e.g required as dev tools by your project!) built on top of Go Modules, allowing reproducible dev environments.", |
| 44 | PersistentPreRunE: func(cmd *cobra.Command, args []string) error { |
| 45 | if len(goCmd) == 0 { |
| 46 | return errors.New("'go' flag cannot be empty") |
| 47 | } |
| 48 | if len(args) > 1 { |
| 49 | return errors.New("too many arguments except none or binary/package") |
| 50 | } |
| 51 | if len(rename) > 0 && len(name) > 0 { |
| 52 | return errors.New("Both -n and -r were specified. You can either rename or create new one.") |
| 53 | } |
| 54 | if len(name) > 0 && !regexp.MustCompile(`[a-zA-Z0-9.-_]+`).MatchString(name) { |
| 55 | return errors.New("-n name contains not allowed characters") |
| 56 | } |
| 57 | if len(rename) > 0 && !regexp.MustCompile(`[a-zA-Z0-9.-_]+`).MatchString(rename) { |
| 58 | return errors.New("-r name contains not allowed characters") |
| 59 | } |
| 60 | return nil |
| 61 | }, |
| 62 | RunE: func(cmd *cobra.Command, args []string) error { |
| 63 | ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) |
| 64 | defer cancel() |
| 65 | |
| 66 | modDirAbs, err := filepath.Abs(moddir) |
| 67 | if err != nil { |
| 68 | return errors.Wrap(err, "abs") |
| 69 | } |
| 70 | defer func() { |
| 71 | if err == nil { |
| 72 | // Leave tmp files on error for debug purposes. |
| 73 | if cerr := cleanGoGetTmpFiles(modDirAbs); cerr != nil { |
| 74 | logger.Println("cannot clean tmp files", err) |
| 75 | } |
| 76 | } |
| 77 | }() |
| 78 | |
| 79 | r, err := runner.NewRunner(ctx, logger, insecure, goCmd) |
| 80 | if err != nil { |
| 81 | return err |
| 82 | } |
| 83 | if verbose { |
| 84 | r.Verbose() |
| 85 | } |
| 86 | |
| 87 | cfg := getConfig{ |
| 88 | runner: r, |
| 89 | modDir: modDirAbs, |
| 90 | relModDir: moddir, |
| 91 | name: name, |
| 92 | rename: rename, |
| 93 | link: link, |
| 94 | timeOut: timeOut, |
| 95 | verbose: verbose, |
| 96 | } |
| 97 | var target string |
| 98 | if len(args) > 0 { |
| 99 | target = args[0] |
| 100 | } |
| 101 | |
| 102 | if err := get(ctx, logger, cfg, target); err != nil { |
| 103 | return errors.Wrap(err, "get") |
| 104 | } |
| 105 | |
| 106 | pkgs, err := bingo.ListPinnedMainPackages(logger, modDirAbs, true) |
| 107 | if err != nil { |
| 108 | return errors.Wrap(err, "list pinned") |
| 109 | } |
| 110 | if len(pkgs) == 0 { |
| 111 | return bingo.RemoveHelpers(modDirAbs) |
| 112 | } |
| 113 | return bingo.GenHelpers(moddir, version.Version, pkgs) |
| 114 | }, |
| 115 | } |
| 116 | flags := cmd.Flags() |
| 117 | flags.StringVarP(&name, "name", "n", "", "The -n flag instructs to get binary and name it with given name instead of default,\n"+ |
| 118 | "so the last element of package directory. Allowed characters [A-z0-9._-]. If -n is used and no package/binary is specified,\n"+ |
| 119 | "bingo get will return error. If -n is used with existing binary name, copy of this binary will be done. Cannot be used with -r") |
| 120 | flags.StringVarP(&rename, "rename", "r", "", "The -r flag instructs to get existing binary and rename it with given name. Allowed characters [A-z0-9._-]. \n"+ |
| 121 | "If -r is used and no package/binary is specified or non existing binary name is used, bingo will return error. Cannot be used with -n.") |
| 122 | flags.StringVar(&goCmd, "go", "go", "Path to the go command.") |
| 123 | flags.BoolVar(&insecure, "insecure", insecure, `Use -insecure flag when using 'go get'`) |
| 124 | flags.BoolVarP(&link, "link", "l", link, "If enabled, bingo will also create soft link called <tool> that links to the current <tool>-<version> binary.\n"+ |
| 125 | "Use Variables.mk and variables.env if you want to be sure that what you are invoking is what is pinned.") |
| 126 | flags.UintVarP(&timeOut, "timeout", "t", 5, "The maximum time (in minutes) to wait for each go command before killing it.\n"+ |
| 127 | "Set this flag to 0 to indefinitely wait on them.") |
| 128 | return cmd |
| 129 | } |
| 130 | |
| 131 | func NewBingoListCommand(logger *log.Logger) *cobra.Command { |
| 132 | cmd := &cobra.Command{ |
| 133 | Use: "list <flags> [<package or binary>]", |
| 134 | Version: version.Version, |
| 135 | Short: "List enumerates all or one binary that are/is currently pinned in this project. ", |
| 136 | Long: "List enumerates all or one binary that are/is currently pinned in this project. It will print exact path, Version and immutable output.", |
| 137 | PersistentPreRunE: func(cmd *cobra.Command, args []string) error { |
| 138 | if len(args) > 1 { |
| 139 | return errors.New("too many arguments except none or binary/package") |
| 140 | } |
| 141 | return nil |
| 142 | }, |
| 143 | RunE: func(cmd *cobra.Command, args []string) error { |
| 144 | modDir, err := filepath.Abs(moddir) |
| 145 | if err != nil { |
| 146 | return errors.Wrap(err, "abs") |
| 147 | } |
| 148 | pkgs, err := bingo.ListPinnedMainPackages(logger, modDir, false) |
| 149 | if err != nil { |
| 150 | return err |
| 151 | } |
| 152 | var target string |
| 153 | if len(args) > 0 { |
| 154 | target = args[0] |
| 155 | } |
| 156 | bingo.SortRenderables(pkgs) |
| 157 | return pkgs.PrintTab(target, os.Stdout) |
| 158 | }, |
| 159 | } |
| 160 | return cmd |
| 161 | } |
| 162 | |
| 163 | func NewBingoVersionCommand() *cobra.Command { |
| 164 | cmd := &cobra.Command{ |
| 165 | Use: "version", |
| 166 | Short: "Prints bingo Version.", |
| 167 | Long: `Prints bingo Version.`, |
| 168 | Run: func(cmd *cobra.Command, args []string) { |
| 169 | _, _ = fmt.Fprintln(os.Stdout, version.Version) |
| 170 | }, |
| 171 | } |
| 172 | return cmd |
| 173 | } |