commands

package
v0.0.0-...-881013b Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Commands []*cli.Command
View Source
var Env = &cli.Command{
	Name:            "env",
	Usage:           "Print the GVM environment variables",
	HideHelpCommand: true,
	ArgsUsage:       " ",
	Before: func(ctx *cli.Context) error {
		env = utils.GvmEnv{}
		env.Init()
		return nil
	},
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:     "shell",
			Usage:    "The shell syntax to use. Print all when missing\noptional shell: bash, zsh, fish, powershell",
			Value:    "bash",
			Required: true,
		},
	},
	Action: func(ctx *cli.Context) error {
		if !ctx.IsSet("shell") {
			return fmt.Errorf("missing shell flag")
		}
		shell := ctx.String("shell")
		var exportStatement string
		switch shell {
		case "bash", "zsh":
			exportStatement = "export %s=\"%s\"\n"
		case "fish":
			exportStatement = "set -gx %s \"%s\";\n"
		case "powershell":
			exportStatement = "$env:%s=\"%s\"\n"
		default:
			return fmt.Errorf("unsupported shell: %s", shell)
		}
		var builder strings.Builder
		for _, env := range env.Envs() {
			builder.WriteString(fmt.Sprintf(exportStatement, env.Name, strings.ReplaceAll(env.Value, "\\", "\\\\")))
		}
		if shell == "zsh" {
			builder.WriteString("rehash\n")
		}
		fmt.Print(builder.String())
		return nil
	},
}
View Source
var Install = &cli.Command{
	Name:    "install",
	Usage:   "Install a version of Go",
	Aliases: []string{"i", "add"},
	Flags: []cli.Flag{
		&cli.BoolFlag{
			Name:    "latest",
			Aliases: []string{"l"},
			Usage:   "Install the latest stable version",
		},
	},
	Action: func(ctx *cli.Context) error {
		versionStr := ctx.Args().First()
		if ctx.Bool("latest") {
			version, err := gvm.LatestStableVersion()
			if err != nil {
				return err
			}
			versionStr = version.Version
		}
		if versionStr == "" {
			return errors.New("no version specified")
		}
		versionStr = strings.TrimPrefix(versionStr, "go")

		fmt.Printf("Installing %s (%s)\n",
			color.BlueString(versionStr),
			runtime.GOARCH,
		)

		bar := pb.Default.New(0).
			Set(pb.Bytes, true).
			Set(pb.SIBytesPrefix, true)
		file, err := gvm.Download("go"+versionStr, os.TempDir(), bar)
		if err != nil {
			return err
		}
		if err = gvm.Install(versionStr, file); err != nil {
			return err
		}
		fmt.Printf("Successfully installed %s\n", color.GreenString(versionStr))
		return nil
	},
}
View Source
var ListLocal = &cli.Command{
	Name:    "list",
	Usage:   "List all locally installed golang versions",
	Aliases: []string{"ls"},
	Action: func(ctx *cli.Context) error {
		versions, _ := gvm.ListLocalVersions()
		currentVersion, isGvm, _ := gvm.CurrentVersion()
		currentVersion = strings.TrimPrefix(currentVersion, "go")

		system := "* system"
		if !isGvm {
			system += " -> " + currentVersion
			system = current(system)
		}
		fmt.Println(system)

		for _, v := range versions {
			version := strings.TrimPrefix(v, "go")
			str := fmt.Sprintf("* %s", version)
			if isGvm && version == currentVersion {
				str = current(str)
			}
			fmt.Println(str)
		}
		return nil
	},
}
View Source
var ListRemote = &cli.Command{
	Name:    "list-remote",
	Usage:   "List remote versions",
	Aliases: []string{"lr"},
	Flags: []cli.Flag{
		&cli.BoolFlag{
			Name:    "all",
			Aliases: []string{"a"},
			Usage:   "Show all remote versions",
			Value:   false,
		},
		&cli.BoolFlag{
			Name:    "stable",
			Aliases: []string{"s"},
			Usage:   "Show only stable versions",
			Value:   true,
		},
	},
	Action: func(ctx *cli.Context) error {
		stable := []bool{ctx.Bool("stable")}
		if ctx.Bool("all") {
			stable = nil
		}
		versions, err := gvm.ListRemote(stable...)
		if err != nil {
			return err
		}
		utils.Reverse(versions)

		for _, v := range versions {
			str := strings.TrimPrefix(v.Version, "go")
			if !v.Stable {
				str = color.YellowString("%s (unstable)", str)
			} else {
				str = color.GreenString(str)
			}
			fmt.Println(str)
		}
		return nil
	},
}
View Source
var Uninstall = &cli.Command{
	Name:    "uninstall",
	Usage:   "Uninstall a version of Go",
	Aliases: []string{"rm", "remove"},
	Action: func(ctx *cli.Context) error {
		versionStr := ctx.Args().First()
		if versionStr == "" {
			return errors.New("no version specified")
		}
		versionStr = strings.TrimPrefix(versionStr, "go")
		fmt.Printf("Uninstalling %s (%s)\n",
			color.BlueString(versionStr),
			runtime.GOARCH,
		)
		if err := gvm.Uninstall(versionStr); err != nil {
			return err
		}
		fmt.Printf("Successfully uninstalled %s\n", color.GreenString(versionStr))
		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