Skip to content
Merged
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
19 changes: 19 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Test

on:
pull_request:
branches: [main]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.24
- name: Build
run: go build -v ./...
- name: Test
run: go test -v ./...
64 changes: 62 additions & 2 deletions cmd/bundle.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package cmd

import (
"fmt"
"os"
"xo/src/bundle"
"xo/src/massdriver"
"xo/src/telemetry"

"github.com/massdriver-cloud/massdriver-sdk-go/massdriver/client"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.opentelemetry.io/otel"
"oras.land/oras-go/v2/content/file"
)

var bundleCmd = &cobra.Command{
Expand All @@ -17,6 +21,12 @@ var bundleCmd = &cobra.Command{
Long: ``,
}

var bundlePullv0Cmd = &cobra.Command{
Use: "pullV0",
Short: "Pulls a bundle from Massdriver",
RunE: runBundlePullV0,
}

var bundlePullCmd = &cobra.Command{
Use: "pull",
Short: "Pulls a bundle from Massdriver",
Expand All @@ -26,10 +36,18 @@ var bundlePullCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(bundleCmd)

bundleCmd.AddCommand(bundlePullv0Cmd)

bundleCmd.AddCommand(bundlePullCmd)
bundlePullCmd.Flags().StringP("organization", "o", "", "Organization slug")
bundlePullCmd.Flags().StringP("tag", "t", "latest", "Bundle tag (defaults to 'latest')")
bundlePullCmd.Flags().StringP("name", "n", "", "Bundle name")
viper.BindPFlag("bundle.tag", bundlePullCmd.Flags().Lookup("tag"))
viper.BindPFlag("bundle.name", bundlePullCmd.Flags().Lookup("name"))
viper.BindPFlag("organization.slug", bundlePullCmd.Flags().Lookup("organization"))
}

func runBundlePull(cmd *cobra.Command, args []string) error {
func runBundlePullV0(cmd *cobra.Command, args []string) error {
ctx, span := otel.Tracer("xo").Start(telemetry.GetContextWithTraceParentFromEnv(), "runBundlePull")
telemetry.SetSpanAttributes(span)
defer span.End()
Expand All @@ -46,11 +64,53 @@ func runBundlePull(cmd *cobra.Command, args []string) error {
defer outFile.Close()

log.Info().Msg("pulling bundle...")
pullErr := bundle.Pull(ctx, client, outFile)
pullErr := bundle.PullV0(ctx, client, outFile)
if pullErr != nil {
return telemetry.LogError(span, pullErr, "an error occurred while pulling bundle")
}
log.Info().Msg("bundle pulled")

return nil
}

func runBundlePull(cmd *cobra.Command, args []string) error {
ctx, span := otel.Tracer("xo").Start(telemetry.GetContextWithTraceParentFromEnv(), "runBundlePull")
telemetry.SetSpanAttributes(span)
defer span.End()

organizationSlug := viper.GetString("organization.slug")
if organizationSlug == "" {
return fmt.Errorf("required flag organization must be set via flag or environment variable")
}
bundleName := viper.GetString("bundle.name")
if bundleName == "" {
return fmt.Errorf("required flag bundleName must be set via flag or environment variable")
}
tag := viper.GetString("bundle.tag")

mdClient, clientErr := client.New()
if clientErr != nil {
return clientErr
}

repo, repoErr := bundle.GetRepo(mdClient, organizationSlug, bundleName)
if repoErr != nil {
return repoErr
}

fileStore, fileErr := file.New("bundle")
if fileErr != nil {
return fileErr
}
defer fileStore.Close()

log.Info().Msg("pulling bundle...")
desc, pullErr := bundle.PullV1(ctx, repo, fileStore, tag)
if pullErr != nil {
return telemetry.LogError(span, pullErr, "an error occurred while pulling bundle")
}
log.Info().Msg("bundle pulled")
log.Debug().Msg("bundle digest: " + desc.Digest.String())

return nil
}
11 changes: 8 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,18 @@ func init() {
cobra.OnInitialize(initLogging)

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.xo.yaml)")
rootCmd.PersistentFlags().Bool("debug", false, "Enable debugging logs")
viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config"))

rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "Log level (error, warn, info, debug)")
viper.BindPFlag("log-level", rootCmd.PersistentFlags().Lookup("log-level"))
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
viper.SetEnvPrefix("MASSDRIVER")
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_"))
viper.AutomaticEnv() // Read in environment variables that match

if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
Expand All @@ -59,8 +66,6 @@ func initConfig() {
viper.SetConfigName(".xo")
}

viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/massdriver-cloud/massdriver-sdk-go v0.0.1-0.20250507003903-77c89b677480
github.com/massdriver-cloud/terraform-config-inspect v0.0.0-20240906041648-e5461c213cea
github.com/mitchellh/go-homedir v1.1.0
github.com/opencontainers/image-spec v1.1.1
github.com/rs/zerolog v1.33.0
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
Expand All @@ -19,6 +20,7 @@ require (
go.opentelemetry.io/otel v1.30.0
go.opentelemetry.io/otel/trace v1.30.0
gopkg.in/yaml.v3 v3.0.1
oras.land/oras-go/v2 v2.6.0
)

require (
Expand All @@ -43,6 +45,7 @@ require (
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
Expand Down Expand Up @@ -83,7 +86,7 @@ require (
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.39.0 // indirect
golang.org/x/sync v0.13.0 // indirect
golang.org/x/sync v0.14.0 // indirect
golang.org/x/sys v0.32.0 // indirect
golang.org/x/text v0.24.0 // indirect
golang.org/x/tools v0.25.0 // indirect
Expand Down
12 changes: 8 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0V
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/massdriver-cloud/mass v0.0.0-20240819225958-4ef28e971211 h1:AipPe343bMha9B5x3xlMUCiKZkyyIRD6D4FXBvbbTbg=
github.com/massdriver-cloud/mass v0.0.0-20240819225958-4ef28e971211/go.mod h1:VJMa3gsc/wmGE61ZicpBQv1xv8JjIPzHpJK2gZYEgqI=
github.com/massdriver-cloud/massdriver-sdk-go v0.0.0 h1:LMqhiFaIQNmnjkiftsXmbEBFSPRuW4ADDbAre+LkdQA=
github.com/massdriver-cloud/massdriver-sdk-go v0.0.0/go.mod h1:NzVlZes2wGSi9JZHeoT1MJ9KIGYO9zICnvoQ7029+GU=
github.com/massdriver-cloud/massdriver-sdk-go v0.0.1-0.20250507003903-77c89b677480 h1:6WLWaj/gngvEGy97CIRNRPHmzfauEahytE+/9nmM/NM=
github.com/massdriver-cloud/massdriver-sdk-go v0.0.1-0.20250507003903-77c89b677480/go.mod h1:NzVlZes2wGSi9JZHeoT1MJ9KIGYO9zICnvoQ7029+GU=
github.com/massdriver-cloud/terraform-config-inspect v0.0.0-20240906041648-e5461c213cea h1:+fS2DYjasqMDMzVeKJTiBLbqYJjv93MTf1yZV2nxjf8=
Expand All @@ -77,6 +75,10 @@ github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQ
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.1.1 h1:y0fUlFfIZhPF1W537XOLg0/fcx6zcHCJwooC2xJA040=
github.com/opencontainers/image-spec v1.1.1/go.mod h1:qpqAh3Dmcf36wStyyWU+kCeDgrGnAve2nCC8+7h8Q0M=
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down Expand Up @@ -190,8 +192,8 @@ golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/net v0.39.0 h1:ZCu7HMWDxpXpaiKdhzIfaltL9Lp31x/3fCP11bc6/fY=
golang.org/x/net v0.39.0/go.mod h1:X7NRbYVEA+ewNkCNyJ513WmMdQ3BineSwVtN2zD/d+E=
golang.org/x/sync v0.13.0 h1:AauUjRAJ9OSnvULf/ARrrVywoJDy0YS2AwQ98I37610=
golang.org/x/sync v0.13.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ=
golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down Expand Up @@ -221,3 +223,5 @@ gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
oras.land/oras-go/v2 v2.6.0 h1:X4ELRsiGkrbeox69+9tzTu492FMUu7zJQW6eJU+I2oc=
oras.land/oras-go/v2 v2.6.0/go.mod h1:magiQDfG6H1O9APp+rOsvCPcW1GD2MM7vgnKY0Y+u1o=
37 changes: 36 additions & 1 deletion src/bundle/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@ import (
"encoding/base64"
"fmt"
"io"
"path/filepath"
"xo/src/api"
"xo/src/massdriver"
"xo/src/telemetry"

"github.com/massdriver-cloud/massdriver-sdk-go/massdriver/client"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"

oras "oras.land/oras-go/v2"
"oras.land/oras-go/v2/registry/remote"
"oras.land/oras-go/v2/registry/remote/auth"
"oras.land/oras-go/v2/registry/remote/retry"
)

func Pull(ctx context.Context, client *massdriver.MassdriverClient, outFile io.Writer) error {
func PullV0(ctx context.Context, client *massdriver.MassdriverClient, outFile io.Writer) error {
_, span := otel.Tracer("xo").Start(ctx, "BundlePull")
telemetry.SetSpanAttributes(span)
defer span.End()
Expand Down Expand Up @@ -41,3 +49,30 @@ func Pull(ctx context.Context, client *massdriver.MassdriverClient, outFile io.W

return nil
}

func PullV1(ctx context.Context, repo oras.Target, target oras.Target, tag string) (v1.Descriptor, error) {
_, span := otel.Tracer("xo").Start(ctx, "BundlePullV1")
telemetry.SetSpanAttributes(span)
defer span.End()

return oras.Copy(ctx, repo, tag, target, tag, oras.DefaultCopyOptions)
}

func GetRepo(mdClient *client.Client, organizationSlug string, bundleName string) (oras.Target, error) {
reg := mdClient.Auth.BaseURL
repo, repoErr := remote.NewRepository(filepath.Join(reg, organizationSlug, bundleName))
if repoErr != nil {
return nil, repoErr
}

repo.Client = &auth.Client{
Client: retry.DefaultClient,
Cache: auth.NewCache(),
Credential: auth.StaticCredential(reg, auth.Credential{
Username: "myuser",
Password: "mypass",
}),
}

return repo, nil
}
Loading