From e48a6bb0cf760cbc1b9abcef682ab5582c767472 Mon Sep 17 00:00:00 2001 From: Tristan Isham Date: Thu, 24 Oct 2024 21:18:26 -0400 Subject: [PATCH] rework command line --- cli/settings.go | 16 ++++---- cli/version.go | 21 +++++----- main.go | 107 ++++++++++++++++++++++-------------------------- 3 files changed, 68 insertions(+), 76 deletions(-) diff --git a/cli/settings.go b/cli/settings.go index 8105e18..df215e3 100644 --- a/cli/settings.go +++ b/cli/settings.go @@ -16,11 +16,11 @@ import ( ) type Settings struct { - path string - VersionMapUrl string `json:"versionMapUrl,omitempty"` - ZlsReleaseWorkerBaseUrl string `json:"zlsReleaseWorkerBaseUrl,omitempty"` - UseColor bool `json:"useColor"` - AlwaysForceInstall bool `json:"alwaysForceInstall"` + path string + VersionMapUrl string `json:"versionMapUrl,omitempty"` + ZlsReleaseWorkerUrl string `json:"zlsReleaseWorkerBaseUrl,omitempty"` + UseColor bool `json:"useColor"` + AlwaysForceInstall bool `json:"alwaysForceInstall"` } func (s *Settings) ToggleColor() { @@ -47,7 +47,7 @@ func (s *Settings) ResetVersionMap() error { } func (s *Settings) ResetZlsReleaseWorkerBaseUrl() error { - s.ZlsReleaseWorkerBaseUrl = "https://releases.zigtools.org" + s.ZlsReleaseWorkerUrl = "https://releases.zigtools.org/v1/zls/index.json" if err := s.save(); err != nil { return err } @@ -98,12 +98,12 @@ func (s *Settings) SetZlsReleaseWorkerBaseUrl(versionMapUrl string) error { return fmt.Errorf("%w: %w", ErrInvalidVersionMap, err) } - s.ZlsReleaseWorkerBaseUrl = versionMapUrl + s.ZlsReleaseWorkerUrl = versionMapUrl if err := s.save(); err != nil { return err } - log.Debug("set zls release worker base url", "url", s.ZlsReleaseWorkerBaseUrl) + log.Debug("set zls release worker base url", "url", s.ZlsReleaseWorkerUrl) return nil } diff --git a/cli/version.go b/cli/version.go index 9377691..360b538 100644 --- a/cli/version.go +++ b/cli/version.go @@ -75,24 +75,23 @@ func (z *ZVM) fetchVersionMap() (zigVersionMap, error) { // note: the zls release-worker uses the same index format as zig, but without the latest master entry. func (z *ZVM) fetchZlsTaggedVersionMap() (zigVersionMap, error) { - log.Debug("initial ZRW","func", "fetchZlsTaggedVersionMap", "url", z.Settings.ZlsReleaseWorkerBaseUrl) + log.Debug("initial ZRW", "func", "fetchZlsTaggedVersionMap", "url", z.Settings.ZlsReleaseWorkerUrl) if err := z.loadSettings(); err != nil { log.Warnf("could not load zls release worker base url from settings: %q", err) - log.Debug("zrw", z.Settings.ZlsReleaseWorkerBaseUrl) + log.Debug("zrw", z.Settings.ZlsReleaseWorkerUrl) } - defaultZrwBaseUrl := "https://releases.zigtools.org" + versionMapUrl := z.Settings.ZlsReleaseWorkerUrl - zrwBaseUrl := z.Settings.ZlsReleaseWorkerBaseUrl + - log.Debug("setting's ZRW", "url", zrwBaseUrl) + log.Debug("setting's ZRW", "url", versionMapUrl) - if len(zrwBaseUrl) == 0 { - zrwBaseUrl = defaultZrwBaseUrl + if len(z.Settings.ZlsReleaseWorkerUrl) == 0 { + versionMapUrl = "https://releases.zigtools.org/v1/zls/index.json" } - versionMapUrl := zrwBaseUrl + "/v1/zls/index.json" req, err := http.NewRequest("GET", versionMapUrl, nil) if err != nil { return nil, err @@ -131,16 +130,16 @@ func (z *ZVM) fetchZlsTaggedVersionMap() (zigVersionMap, error) { // note: the zls release-worker uses the same index format as zig, but without the latest master entry. // this function does not write the result to a file. func (z *ZVM) fetchZlsVersionByZigVersion(version string, compatMode string) (zigVersion, error) { - log.Debug("initial ZRW","func", "fetchZlsVersionByZigVersion", "url", z.Settings.ZlsReleaseWorkerBaseUrl) + log.Debug("initial ZRW", "func", "fetchZlsVersionByZigVersion", "url", z.Settings.ZlsReleaseWorkerUrl) if err := z.loadSettings(); err != nil { log.Warnf("could not load zls release worker base url from settings: %q", err) - log.Debug("zrw", z.Settings.ZlsReleaseWorkerBaseUrl) + log.Debug("zrw", z.Settings.ZlsReleaseWorkerUrl) } defaultZrwBaseUrl := "https://releases.zigtools.org" - zrwBaseUrl := z.Settings.ZlsReleaseWorkerBaseUrl + zrwBaseUrl := z.Settings.ZlsReleaseWorkerUrl log.Debug("setting's ZRW", "url", zrwBaseUrl) diff --git a/main.go b/main.go index e440286..d20e247 100644 --- a/main.go +++ b/main.go @@ -156,23 +156,6 @@ var zvmApp = &opts.App{ } }, }, - // { - // Name: "list:all", - // Usage: "list remote Zig versions available for download, based on your version map", - // Aliases: []string{"la"}, - // Args: false, - // // Flags: []opts.Flag{ - // // &opts.BoolFlag{ - // // Name: "all", - // // Aliases: []string{"a"}, - // // Usage: "list remote Zig versions available for download", - // // }, - // // }, - // Action: func(ctx *opts.Context) error { - // log.Debug("Version Map", "url", zvm.Settings.VersionMapUrl, "cmd", "la") - // return zvm.ListRemoteAvailable() - // }, - // }, { Name: "uninstall", Usage: "remove an installed version of Zig", @@ -199,53 +182,63 @@ var zvmApp = &opts.App{ }, }, { - Name: "vmu", - Usage: "set ZVM's version map URL for custom Zig distribution servers", - Args: true, - Action: func(ctx *opts.Context) error { - url := ctx.Args().First() - log.Debug("user passed vmu", "url", url) + Name: "source", + Aliases: []string{"src"}, + Usage: "set ZVM's version map URL for custom Zig distribution servers", + Args: true, + Subcommands: []*opts.Command{ + { + Name: "zig", + Usage: "set ZVM's version map URL for custom Zig distribution servers", + Args: true, + ArgsUsage: "", - switch url { - case "default": - return zvm.Settings.ResetVersionMap() + Action: func(ctx *opts.Context) error { + url := ctx.Args().First() + log.Debug("user passed VMU", "url", url) - case "mach": - if err := zvm.Settings.SetVersionMapUrl("https://machengine.org/zig/index.json"); err != nil { - log.Info("Run `zvm vmu default` to reset your version map.") - return err - } + switch url { + case "default": + return zvm.Settings.ResetVersionMap() - default: - if err := zvm.Settings.SetVersionMapUrl(url); err != nil { - log.Info("Run `zvm vmu default` to reset your verison map.") - return err - } - } + case "mach": + if err := zvm.Settings.SetVersionMapUrl("https://machengine.org/zig/index.json"); err != nil { + log.Info("Run `zvm source:zig default` to reset your version map.") + return err + } - return nil - }, - }, - { - Name: "zrw", - Usage: "set ZVM's URL for custom Zls Release Workers", - Args: true, - Action: func(ctx *opts.Context) error { - url := ctx.Args().First() - log.Debug("user passed zrw", "url", url) + default: + if err := zvm.Settings.SetVersionMapUrl(url); err != nil { + log.Info("Run `zvm source:zig default` to reset your verison map.") + return err + } + } - switch url { - case "default": - return zvm.Settings.ResetZlsReleaseWorkerBaseUrl() + return nil + }, + }, + { + Name: "zls", + Usage: "set ZVM's URL for custom ZLS Release Workers", + Args: true, + Action: func(ctx *opts.Context) error { + url := ctx.Args().First() + log.Debug("user passed zrw", "url", url) - default: - if err := zvm.Settings.SetZlsReleaseWorkerBaseUrl(url); err != nil { - log.Info("Run `zvm zrw default` to reset your release worker.") - return err - } - } + switch url { + case "default": + return zvm.Settings.ResetZlsReleaseWorkerBaseUrl() - return nil + default: + if err := zvm.Settings.SetZlsReleaseWorkerBaseUrl(url); err != nil { + log.Info("Run `zvm zrw default` to reset your release worker.") + return err + } + } + + return nil + }, + }, }, }, },