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

feat: add shuttle extensions cli commands #220

Open
wants to merge 4 commits into
base: feat/shuttle-extensions
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ func initializedRootFromArgs(stdout, stderr io.Writer, args []string) (*cobra.Co
// Run and LS will not get closured variables from contextProvider
rootCmd.ParseFlags(args)

rootCmd.AddCommand(newExtCmd())
if err := addExtensions(rootCmd); err != nil {
uii.Verboseln("failed to register extensions: %s", err.Error())
}

if isInRepoContext() {
runCmd, err := newRun(uii, ctxProvider)
if err != nil {
Expand Down
172 changes: 172 additions & 0 deletions cmd/ext.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package cmd

import (
"errors"
"os"
"os/exec"

stdcontext "context"

"github.com/lunarway/shuttle/internal/extensions"
"github.com/lunarway/shuttle/internal/global"
"github.com/spf13/cobra"
)

type extGlobalConfig struct {
registry string
}

func (c *extGlobalConfig) getRegistry() (string, bool) {
if c.registry != "" {
return c.registry, true
}

if registryEnv := os.Getenv("SHUTTLE_EXTENSIONS_REGISTRY"); registryEnv != "" {
return registryEnv, true
}

return "", false
}

func addExtensions(rootCmd *cobra.Command) error {
extManager := extensions.NewExtensionsManager(global.NewGlobalStore())

extensions, err := extManager.GetAll(stdcontext.Background())
if err != nil {
return err
}
grp := &cobra.Group{
ID: "extensions",
Title: "Extensions",
}
rootCmd.AddGroup(grp)
for _, extension := range extensions {
extension := extension

rootCmd.AddCommand(
&cobra.Command{
Use: extension.Name(),
Short: extension.Description(),
Version: extension.Version(),
GroupID: "extensions",
DisableFlagParsing: true,
RunE: func(cmd *cobra.Command, args []string) error {
extCmd := exec.CommandContext(cmd.Context(), extension.FullPath(), args...)

extCmd.Stdout = os.Stdout
extCmd.Stderr = os.Stderr
extCmd.Stdin = os.Stdin

if err := extCmd.Start(); err != nil {
return err
}

if err := extCmd.Wait(); err != nil {
return err
}

return nil
},
},
)
}

return nil
}

func newExtCmd() *cobra.Command {
globalConfig := &extGlobalConfig{}

cmd := &cobra.Command{
Use: "ext",
Long: "helps you manage shuttle extensions",
}

cmd.AddCommand(
newExtInstallCmd(globalConfig),
newExtUpdateCmd(globalConfig),
newExtInitCmd(globalConfig),
newExtPublishCmd(globalConfig),
)

cmd.PersistentFlags().StringVar(&globalConfig.registry, "registry", "", "the given registry, if not set will default to SHUTTLE_EXTENSIONS_REGISTRY")

return cmd
}

func newExtInstallCmd(globalConfig *extGlobalConfig) *cobra.Command {
cmd := &cobra.Command{
Use: "install",
Long: "Install ensures that extensions are downloaded and available",
RunE: func(cmd *cobra.Command, args []string) error {
extManager := extensions.NewExtensionsManager(global.NewGlobalStore())

if err := extManager.Install(cmd.Context()); err != nil {
return err
}

return nil
},
}

return cmd
}

func newExtUpdateCmd(globalConfig *extGlobalConfig) *cobra.Command {
cmd := &cobra.Command{
Use: "update",
Short: "Update will fetch the latest version of the extensions from the given registry",
RunE: func(cmd *cobra.Command, args []string) error {
extManager := extensions.NewExtensionsManager(global.NewGlobalStore())

registry, ok := globalConfig.getRegistry()
if !ok {
return errors.New("registry is not set")
}

if err := extManager.Update(cmd.Context(), registry); err != nil {
return err
}

return nil
},
}

return cmd
}

func newExtInitCmd(globalConfig *extGlobalConfig) *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Short: "init will create an initial extensions repository",
RunE: func(cmd *cobra.Command, args []string) error {
return nil
},
}

return cmd
}

func newExtPublishCmd(globalConfig *extGlobalConfig) *cobra.Command {
var version string

// Publish can either be called by a user to rollback an extension, or by CI to automatically publish an extension.
cmd := &cobra.Command{
Use: "publish",
Short: "Publishes the current extension to a registry",
RunE: func(cmd *cobra.Command, args []string) error {
extManager := extensions.NewExtensionsManager(global.NewGlobalStore())

if err := extManager.Publish(cmd.Context(), version); err != nil {
return err
}

return nil
},
}

cmd.Flags().StringVar(&version, "version", "", "the version to publish")
cmd.MarkFlagRequired("version")

return cmd
}
75 changes: 75 additions & 0 deletions internal/extensions/downloader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package extensions

import (
"context"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
)

type Downloader interface {
Download(ctx context.Context, dest string) error
}

func NewDownloader(downloadLink *registryExtensionDownloadLink) (Downloader, error) {
switch downloadLink.Provider {
case "github-release":
return newGitHubReleaseDownloader(downloadLink), nil
default:
return nil, fmt.Errorf("invalid provider type: %s", downloadLink.Provider)
}
}

type gitHubReleaseDownloader struct {
link *registryExtensionDownloadLink
}

func newGitHubReleaseDownloader(downloadLink *registryExtensionDownloadLink) Downloader {
return &gitHubReleaseDownloader{
link: downloadLink,
}
}

func (d *gitHubReleaseDownloader) Download(ctx context.Context, dest string) error {
client := http.DefaultClient
client.Timeout = time.Second * 60

req, err := http.NewRequestWithContext(ctx, http.MethodGet, d.link.Url, nil)
if err != nil {
return err
}

bearer, err := getGithubToken()
if err != nil {
return err
}

req.Header.Add("Authorization", fmt.Sprintf("token %s", bearer))
req.Header.Add("Accept", "application/octet-stream")

resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

if err := os.RemoveAll(dest); err != nil {
log.Printf("failed to remove extension before downloading new: %s, please try again", err.Error())
}

extensionBinary, err := os.Create(dest)
if err != nil {
return err
}
defer extensionBinary.Close()
extensionBinary.Chmod(0o755)

if _, err := io.Copy(extensionBinary, resp.Body); err != nil {
return err
}

return nil
}
88 changes: 88 additions & 0 deletions internal/extensions/extension.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package extensions

import (
"context"
"fmt"
"path"
"runtime"

"github.com/lunarway/shuttle/internal/global"
)

// Extension is the descriptor of a single extension, it is used to add description to the cli, as well as calling the specific extension in question
type Extension struct {
os string
arch string
globalStore *global.GlobalStore
remote *registryExtension
}

func newExtensionFromRegistry(globalStore *global.GlobalStore, registryExtension *registryExtension) (*Extension, error) {
return &Extension{
os: runtime.GOOS,
arch: runtime.GOARCH,
globalStore: globalStore,
remote: registryExtension,
}, nil
}

func (e *Extension) Ensure(ctx context.Context) error {
extensionsCachePath := getExtensionsCachePath(e.globalStore)
binaryName := e.getExtensionBinaryName()
if err := ensureExists(extensionsCachePath); err != nil {
return fmt.Errorf("failed to create cache path: %w", err)
}

binaryPath := path.Join(extensionsCachePath, binaryName)
if exists(binaryPath) {
// TODO: do a checksum chck
//return nil
}

downloadLink := e.getRemoteBinaryDownloadLink()
if downloadLink == nil {
return fmt.Errorf("failed to find a valid extension matching your os and architecture")
}

downloader, err := NewDownloader(downloadLink)
if err != nil {
return err
}

if err := downloader.Download(ctx, binaryPath); err != nil {
return err
}

return nil
}

func (e *Extension) Name() string {
return e.remote.Name
}

func (e *Extension) Version() string {
return e.remote.Version
}

func (e *Extension) Description() string {
return e.remote.Description
}

func (e *Extension) getExtensionBinaryName() string {
return e.remote.Name
}

func (e *Extension) FullPath() string {
return path.Join(getExtensionsCachePath(e.globalStore), e.Name())
}

func (e *Extension) getRemoteBinaryDownloadLink() *registryExtensionDownloadLink {
for _, download := range e.remote.DownloadUrls {
if download.Os == e.os &&
download.Architecture == e.arch {
return &download
}
}

return nil
}
44 changes: 44 additions & 0 deletions internal/extensions/extension_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package extensions

import (
"context"
"fmt"
"os"

"gopkg.in/yaml.v2"
)

type shuttleExtensionsRegistry struct {
GitHub *string `json:"github" yaml:"github"`
}

type shuttleExtensionProviderGitHubRelease struct {
Owner string `json:"owner" yaml:"owner"`
Repo string `json:"repo" yaml:"repo"`
}

type shuttleExtensionsProvider struct {
GitHubRelease *shuttleExtensionProviderGitHubRelease `json:"github-release" yaml:"github-release"`
}

type shuttleExtensionsFile struct {
Name string `json:"name" yaml:"name"`
Description string `json:"description" yaml:"description"`

Provider shuttleExtensionsProvider `json:"provider" yaml:"provider"`
Registry shuttleExtensionsRegistry `json:"registry" yaml:"registry"`
}

func getExtensionsFile(_ context.Context) (*shuttleExtensionsFile, error) {
templateFileContent, err := os.ReadFile("shuttle.template.yaml")
if err != nil {
return nil, fmt.Errorf("failed to find shuttle.template.yaml: %w", err)
}

var templateFile shuttleExtensionsFile
if err := yaml.Unmarshal(templateFileContent, &templateFile); err != nil {
return nil, fmt.Errorf("failed to parse shuttle.template.yaml: %w", err)
}

return &templateFile, nil
}
Loading
Loading