Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Install a specific provider version #1918

Merged
merged 8 commits into from
Sep 26, 2023
27 changes: 25 additions & 2 deletions apps/cnquery/cmd/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var listProvidersCmd = &cobra.Command{
}

var installProviderCmd = &cobra.Command{
Use: "install <NAME>",
Use: "install <NAME[@VERSION]>",
Short: "Install or update a provider.",
Long: "",
PreRun: func(cmd *cobra.Command, args []string) {},
Expand All @@ -68,10 +68,33 @@ var installProviderCmd = &cobra.Command{
return
}

log.Fatal().Msg("cannot install providers by name yet")
if len(args) == 0 {
log.Fatal().Msg("no provider specified, use the NAME[@VERSION] format to pass in a provider name")
}

// if no url or file is specified, we default to installing by name from the default upstream
installProviderByName(args[0])
},
}

func installProviderByName(name string) {
parts := strings.Split(name, "@")
if len(parts) > 2 {
log.Fatal().Msg("invalid provider name")
}
name = parts[0]
version := ""
if len(parts) == 2 {
// trim the v prefix, allowing users to specify both 9.0.0 and v9.0.0
version = strings.TrimPrefix(parts[1], "v")
}
installed, err := providers.Install(name, version)
if err != nil {
log.Fatal().Err(err).Msg("failed to install")
}
providers.PrintInstallResults([]*providers.Provider{installed})
}

func installProviderUrl(u string) {
if i := strings.Index(u, "://"); i == -1 {
u = "http://" + u
Expand Down
2 changes: 1 addition & 1 deletion providers/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func (c *coordinator) tryProviderUpdate(provider *Provider, update UpdateProvide
if err != nil {
return nil, err
}

PrintInstallResults([]*Provider{provider})
now := time.Now()
if err := os.Chtimes(binPath, now, now); err != nil {
log.Warn().
Expand Down
30 changes: 22 additions & 8 deletions providers/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,23 @@ func EnsureProvider(existing Providers, connectorName string, connectorType stri
return nil, errors.New("cannot find installed provider for connection " + connectorName)
}

nu, err := Install(upstream.Name)
nu, err := Install(upstream.Name, "")
if err != nil {
return nil, err
}
existing.Add(nu)
return nu, err
PrintInstallResults([]*Provider{nu})
return nu, nil
}

func Install(name string) (*Provider, error) {
version, err := LatestVersion(name)
if err != nil {
return nil, err
func Install(name string, version string) (*Provider, error) {
if version == "" {
// if no version is specified, we default to installing the latest one
latestVersion, err := LatestVersion(name)
if err != nil {
return nil, err
}
version = latestVersion
}

log.Info().
Expand All @@ -205,10 +213,17 @@ func installVersion(name string, version string) (*Provider, error) {
log.Debug().Str("url", url).Msg("installing provider from URL")
res, err := http.Get(url)
if err != nil {
log.Debug().Str("url", url).Msg("failed to install form URL (get request)")
log.Debug().Str("url", url).Msg("failed to install from URL (get request)")
return nil, errors.Wrap(err, "failed to install "+name+"-"+version)
}
if res.StatusCode == http.StatusNotFound {
return nil, errors.New("cannot find provider " + name + "-" + version + " under url " + url)
} else if res.StatusCode != http.StatusOK {
log.Debug().Str("url", url).Int("status", res.StatusCode).Msg("failed to install from URL (status code)")
return nil, errors.New("failed to install " + name + "-" + version + ", received status code: " + res.Status)
}

// else we know we got a 200 response, we can safely install
installed, err := InstallIO(res.Body, InstallConf{
Dst: DefaultPath,
})
Expand All @@ -231,7 +246,6 @@ func installVersion(name string, version string) (*Provider, error) {
// otherwise it will load old data
CachedProviders = nil

PrintInstallResults(installed)
return installed[0], nil
}

Expand Down