Skip to content

Commit

Permalink
rework command line
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanisham committed Oct 25, 2024
1 parent fe5dec3 commit e48a6bb
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 76 deletions.
16 changes: 8 additions & 8 deletions cli/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
21 changes: 10 additions & 11 deletions cli/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
107 changes: 50 additions & 57 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
},
},
},
},
},
Expand Down

0 comments on commit e48a6bb

Please sign in to comment.