Skip to content

Commit

Permalink
feat: with github registry
Browse files Browse the repository at this point in the history
Signed-off-by: Kasper J. Hermansen <[email protected]>
  • Loading branch information
kjuulh committed Jan 26, 2024
1 parent 3eeb1e5 commit a04e066
Show file tree
Hide file tree
Showing 11 changed files with 471 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func initializedRootFromArgs(stdout, stderr io.Writer, args []string) (*cobra.Co

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

if isInRepoContext() {
Expand Down
26 changes: 25 additions & 1 deletion cmd/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func addExtensions(rootCmd *cobra.Command) error {
Version: extension.Version(),
GroupID: "extensions",
DisableFlagParsing: true,
RunE: func(cmd *cobra.Command, args []string) error {

RunE: func(cmd *cobra.Command, args []string) error {
extCmd := exec.CommandContext(cmd.Context(), extension.FullPath(), args...)

extCmd.Stdout = os.Stdout
Expand Down Expand Up @@ -87,6 +87,7 @@ func newExtCmd() *cobra.Command {
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")
Expand Down Expand Up @@ -146,3 +147,26 @@ func newExtInitCmd(globalConfig *extGlobalConfig) *cobra.Command {

return cmd
}

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

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
}
6 changes: 6 additions & 0 deletions internal/extensions/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
Expand Down Expand Up @@ -54,11 +55,16 @@ func (d *gitHubReleaseDownloader) Download(ctx context.Context, dest string) err
}
defer resp.Body.Close()

if err := os.RemoveAll(dest); err != nil {
log.Printf("failed to remove extension before downloading new: %s", 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
Expand Down
3 changes: 2 additions & 1 deletion internal/extensions/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ func (e *Extension) Ensure(ctx context.Context) error {

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

downloadLink := e.getRemoteBinaryDownloadLink()
Expand Down
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(ctx 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
}
20 changes: 19 additions & 1 deletion internal/extensions/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (e *ExtensionsManager) Install(ctx context.Context) error {

// Update will fetch the latest extensions from a registry and install them afterwards so that they're ready for use
func (e *ExtensionsManager) Update(ctx context.Context, registry string) error {
reg, err := NewRegistry(registry, e.globalStore)
reg, err := NewRegistryFromCombined(registry, e.globalStore)
if err != nil {
return fmt.Errorf("failed to update extensions: %w", err)
}
Expand All @@ -91,3 +91,21 @@ func (e *ExtensionsManager) Update(ctx context.Context, registry string) error {

return nil
}

func (e *ExtensionsManager) Publish(ctx context.Context, version string) error {
extensionsFile, err := getExtensionsFile(ctx)
if err != nil {
return err
}

registry, err := NewRegistry("github", "", e.globalStore)
if err != nil {
return err
}

if err := registry.Publish(ctx, extensionsFile, version); err != nil {
return err
}

return nil
}
5 changes: 5 additions & 0 deletions internal/extensions/git_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ type gitRegistry struct {
globalStore *global.GlobalStore
}

// Publish isn't implemented yet for gitRegistry
func (*gitRegistry) Publish(ctx context.Context, extFile *shuttleExtensionsFile, version string) error {
panic("unimplemented")
}

func (*gitRegistry) Get(ctx context.Context) error {
panic("unimplemented")
}
Expand Down
Loading

0 comments on commit a04e066

Please sign in to comment.