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
27 changes: 5 additions & 22 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,30 @@ INSTALL_PATH ?= ~/bin
GIT_SHA := $(shell git log -1 --pretty=format:"%H")
LD_FLAGS := "-X github.com/massdriver-cloud/mass/internal/version.version=dev -X github.com/massdriver-cloud/mass/internal/version.gitSHA=local-dev-${GIT_SHA}"

MASSDRIVER_PATH?=../massdriver
MKFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
MKFILE_DIR := $(dir $(MKFILE_PATH))
API_DIR := internal/api
SCHEMA_URL ?= https://api.massdriver.cloud/graphql/v2/schema.graphql

.DEFAULT_GOAL := install

all.macos: clean generate install.macos
all.linux: clean generate install.linux

.PHONY: check
check: clean generate test ## Run tests and linter locally
golangci-lint run
check: test lint ## Run tests and linter locally

.PHONY: clean
clean:
rm -rf ${API_DIR}/schema.graphql
rm -rf ${API_DIR}/zz_generated.go
rm -f ./mass
rm -rf bin/

.PHONY: docs
docs: build
./mass docs

.PHONY: generate
generate:
curl -s ${SCHEMA_URL} -o ${API_DIR}/schema.graphql
cd ${API_DIR} && go generate

.PHONY: test
test:
go test ./... -cover

bin:
mkdir bin

.PHONY: lint
lint:
golangci-lint run

bin:
mkdir bin

.PHONY: build
build:
@if [ "$$(uname -s)" = "Darwin" ]; then \
Expand Down
109 changes: 47 additions & 62 deletions cmd/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,23 @@ import (
"fmt"
"os"
"path/filepath"
"slices"
"sort"
"strings"
"text/template"
"time"

"github.com/charmbracelet/glamour"
"github.com/massdriver-cloud/mass/docs/helpdocs"
"github.com/massdriver-cloud/mass/internal/api"
"github.com/massdriver-cloud/mass/internal/bundle"
"github.com/massdriver-cloud/mass/internal/cli"
cmdbundle "github.com/massdriver-cloud/mass/internal/commands/bundle"
"github.com/massdriver-cloud/mass/internal/params"
"github.com/massdriver-cloud/mass/internal/prettylogs"
"github.com/massdriver-cloud/mass/internal/resourcetype"
"github.com/massdriver-cloud/mass/internal/templates"
"github.com/massdriver-cloud/massdriver-sdk-go/massdriver/client"
"github.com/massdriver-cloud/massdriver-sdk-go/massdriver"
"github.com/massdriver-cloud/massdriver-sdk-go/massdriver/platform/ocirepos"
"github.com/massdriver-cloud/massdriver-sdk-go/massdriver/platform/types"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -196,12 +198,12 @@ func runBundleCreate(cmd *cobra.Command, args []string) error {

cmd.SilenceUsage = true

mdClient, mdClientErr := client.New()
if mdClientErr != nil {
return fmt.Errorf("error initializing massdriver client: %w", mdClientErr)
mdClient, err := massdriver.NewClient()
if err != nil {
return fmt.Errorf("error initializing massdriver client: %w", err)
}

return createOciRepoCommon(ctx, mdClient, name, "bundle", attrs)
return createOciRepoCommon(ctx, mdClient, name, string(ocirepos.ArtifactTypeBundle), attrs)
}

func runBundleTemplateList(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -268,12 +270,12 @@ func runBundleNew(input *bundleNew) error {
var runErr error
if input.name == "" || input.templateName == "" {
// run the interactive prompt
mdClient, mdClientErr := client.New()
if mdClientErr != nil {
return fmt.Errorf("error initializing massdriver client: %w", mdClientErr)
mdClient, err := massdriver.NewClient()
if err != nil {
return fmt.Errorf("error initializing massdriver client: %w", err)
}

resourceTypes, listErr := api.ListResourceTypes(ctx, mdClient, nil)
resourceTypes, listErr := resourcetype.List(ctx, mdClient)
if listErr != nil {
return fmt.Errorf("error fetching resource types: %w", listErr)
}
Expand All @@ -284,7 +286,7 @@ func runBundleNew(input *bundleNew) error {
resourceTypeNames[i] += " (" + rt.Name + ")"
}
}
slices.Sort(resourceTypeNames)
sort.Strings(resourceTypeNames)

templateData, runErr = runBundleNewInteractive(input.outputDir, resourceTypeNames)
if runErr != nil {
Expand Down Expand Up @@ -338,9 +340,9 @@ func runBundleBuild(cmd *cobra.Command, args []string) error {
return err
}

mdClient, mdClientErr := client.New()
if mdClientErr != nil {
return fmt.Errorf("error initializing massdriver client: %w", mdClientErr)
mdClient, err := massdriver.NewClient()
if err != nil {
return fmt.Errorf("error initializing massdriver client: %w", err)
}

return cmdbundle.RunBuild(bundleDirectory, unmarshalledBundle, mdClient)
Expand Down Expand Up @@ -372,12 +374,12 @@ func runBundleLint(cmd *cobra.Command, args []string) error {
return err
}

mdClient, mdClientErr := client.New()
if mdClientErr != nil {
return fmt.Errorf("error initializing massdriver client: %w", mdClientErr)
mdClient, err := massdriver.NewClient()
if err != nil {
return fmt.Errorf("error initializing massdriver client: %w", err)
}

err = unmarshalledBundle.DereferenceSchemas(bundleDirectory, mdClient)
err = unmarshalledBundle.DereferenceSchemas(bundleDirectory, resourcetype.NewMassdriverResolver(mdClient))
if err != nil {
return err
}
Expand Down Expand Up @@ -427,12 +429,12 @@ func runBundlePublish(cmd *cobra.Command, args []string) error {
return err
}

mdClient, mdClientErr := client.New()
if mdClientErr != nil {
return fmt.Errorf("error initializing massdriver client: %w", mdClientErr)
mdClient, err := massdriver.NewClient()
if err != nil {
return fmt.Errorf("error initializing massdriver client: %w", err)
}

err = unmarshalledBundle.Build(bundleDirectory, mdClient)
err = unmarshalledBundle.Build(bundleDirectory, resourcetype.NewMassdriverResolver(mdClient))
if err != nil {
return err
}
Expand Down Expand Up @@ -483,9 +485,9 @@ func runBundlePull(cmd *cobra.Command, args []string) error {
}
}

mdClient, mdClientErr := client.New()
if mdClientErr != nil {
return mdClientErr
mdClient, err := massdriver.NewClient()
if err != nil {
return fmt.Errorf("error initializing massdriver client: %w", err)
}

pullErr := cmdbundle.RunPull(ctx, mdClient, bundleName, version, directory)
Expand All @@ -499,33 +501,28 @@ func runBundlePull(cmd *cobra.Command, args []string) error {
func runBundleList(input *bundleList) error {
ctx := context.Background()

mdClient, err := client.New()
mdClient, err := massdriver.NewClient()
if err != nil {
return fmt.Errorf("error initializing massdriver client: %w", err)
}

filter := &api.OciReposFilter{
ArtifactType: "application/vnd.massdriver.bundle.v1+json",
listInput := ocirepos.ListInput{
ArtifactType: ocirepos.ArtifactTypeBundle,
Search: input.search,
NameEquals: input.name,
}
if input.name != "" {
filter.Name = &api.OciRepoNameFilter{Eq: input.name}
}

var sort *api.OciReposSort
if input.sortField != "" {
order := api.SortOrderAsc
listInput.SortOrder = ocirepos.SortAsc
if strings.EqualFold(input.sortOrder, "desc") {
order = api.SortOrderDesc
listInput.SortOrder = ocirepos.SortDesc
}
field := api.OciReposSortFieldName
listInput.SortBy = ocirepos.SortByName
if strings.EqualFold(input.sortField, "created_at") {
field = api.OciReposSortFieldCreatedAt
listInput.SortBy = ocirepos.SortByCreatedAt
}
sort = &api.OciReposSort{Field: field, Order: order}
}

repos, err := api.ListOciRepos(ctx, mdClient, filter, sort)
repos, err := mdClient.OciRepos.List(ctx, listInput)
if err != nil {
return fmt.Errorf("failed to list bundles: %w", err)
}
Expand All @@ -540,15 +537,7 @@ func runBundleList(input *bundleList) error {
case "table":
tbl := cli.NewTable("Name", "Latest", "Created At")
for _, repo := range repos {
latest := ""
for _, rc := range repo.ReleaseChannels {
if rc.Name == "latest" {
latest = rc.Tag
break
}
}
createdAt := repo.CreatedAt.Format("2006-01-02 15:04:05")
tbl.AddRow(repo.Name, latest, createdAt)
tbl.AddRow(repo.Name, repo.LatestTag, repo.CreatedAt.Format("2006-01-02 15:04:05"))
}
tbl.Print()
default:
Expand All @@ -572,25 +561,25 @@ func runBundleGet(cmd *cobra.Command, args []string) error {
}
cmd.SilenceUsage = true

mdClient, mdClientErr := client.New()
if mdClientErr != nil {
return fmt.Errorf("error initializing massdriver client: %w", mdClientErr)
mdClient, err := massdriver.NewClient()
if err != nil {
return fmt.Errorf("error initializing massdriver client: %w", err)
}

bundle, err := api.GetBundle(ctx, mdClient, bundleID)
b, err := mdClient.Bundles.Get(ctx, bundleID)
if err != nil {
return err
}

switch outputFormat {
case "json":
jsonBytes, marshalErr := json.MarshalIndent(bundle, "", " ")
jsonBytes, marshalErr := json.MarshalIndent(b, "", " ")
if marshalErr != nil {
return fmt.Errorf("failed to marshal bundle to JSON: %w", marshalErr)
}
fmt.Println(string(jsonBytes))
case "text":
err = renderBundle(bundle, mdClient)
err = renderBundle(b, mdClient)
if err != nil {
return err
}
Expand All @@ -601,7 +590,7 @@ func runBundleGet(cmd *cobra.Command, args []string) error {
return nil
}

func renderBundle(b *api.Bundle, mdClient *client.Client) error {
func renderBundle(b *types.Bundle, mdClient *massdriver.Client) error {
tmplBytes, err := bundleTemplates.ReadFile("templates/bundle.get.md.tmpl")
if err != nil {
return fmt.Errorf("failed to read template: %w", err)
Expand All @@ -614,14 +603,10 @@ func renderBundle(b *api.Bundle, mdClient *client.Client) error {

// Get app URL for constructing bundle URL
ctx := context.Background()
urlHelper, urlErr := api.NewURLHelper(ctx, mdClient)
bundleURL := ""
if urlErr == nil {
bundleURL = urlHelper.BundleURL(b.Name, b.Version)
}
bundleURL := mdClient.URLs.Helper(ctx).BundleURL(b.Name, b.Version)

data := struct {
*api.Bundle
*types.Bundle
URL string
FormatTime func(time.Time) string
}{
Expand Down
Loading
Loading