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
10 changes: 9 additions & 1 deletion cmd/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,15 @@ func runEnvironmentList(cmd *cobra.Command, args []string) error {
tbl := cli.NewTable("ID/Slug", "Name", "Description", "Monthly $", "Daily $")

for _, env := range environments {
tbl.AddRow(env.Slug, env.Name, env.Description, env.Cost.Monthly.Average.Amount, env.Cost.Daily.Average.Amount)
monthly := ""
daily := ""
if env.Cost.Monthly.Average.Amount != nil {
monthly = fmt.Sprintf("%v", *env.Cost.Monthly.Average.Amount)
}
if env.Cost.Daily.Average.Amount != nil {
daily = fmt.Sprintf("%v", *env.Cost.Daily.Average.Amount)
}
tbl.AddRow(env.Slug, env.Name, env.Description, monthly, daily)
}

tbl.Print()
Expand Down
10 changes: 9 additions & 1 deletion cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,15 @@ func runProjectList(cmd *cobra.Command, args []string) error {
tbl := cli.NewTable("ID/Slug", "Name", "Description", "Monthly $", "Daily $")

for _, project := range projects {
tbl.AddRow(project.Slug, project.Name, project.Description, project.Cost.Monthly.Average.Amount, project.Cost.Daily.Average.Amount)
monthly := ""
daily := ""
if project.Cost.Monthly.Average.Amount != nil {
monthly = fmt.Sprintf("%v", *project.Cost.Monthly.Average.Amount)
}
if project.Cost.Daily.Average.Amount != nil {
daily = fmt.Sprintf("%v", *project.Cost.Daily.Average.Amount)
}
tbl.AddRow(project.Slug, project.Name, project.Description, monthly, daily)
}

tbl.Print()
Expand Down
4 changes: 2 additions & 2 deletions cmd/templates/environment.get.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
{{- end}}

## Cost
**Monthly Average:** ${{.Cost.Monthly.Average.Amount}}
**Monthly Average:** {{with .Monthly.Average.Amount}}${{.}}{{end}}

**Daily Average:** ${{.Cost.Daily.Average.Amount}}
**Daily Average:** {{with .Daily.Average.Amount}}${{.}}{{end}}
4 changes: 2 additions & 2 deletions cmd/templates/project.get.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
{{end}}

## Cost
**Monthly Average:** ${{.Cost.Monthly.Average.Amount}}
**Monthly Average:** {{with .Monthly.Average.Amount}}${{.}}{{end}}

**Daily Average:** ${{.Cost.Daily.Average.Amount}}
**Daily Average:** {{with .Daily.Average.Amount}}${{.}}{{end}}
26 changes: 19 additions & 7 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package cmd

import (
"context"
"fmt"

"github.com/massdriver-cloud/mass/pkg/api"
"github.com/massdriver-cloud/mass/pkg/prettylogs"
"github.com/massdriver-cloud/mass/pkg/version"
"github.com/massdriver-cloud/massdriver-sdk-go/massdriver/client"
"github.com/spf13/cobra"
)

Expand All @@ -21,15 +24,24 @@ func NewCmdVersion() *cobra.Command {
}

func runVersion(cmd *cobra.Command, args []string) {
latestVersion, err := version.GetLatestVersion()
massVersionColor := prettylogs.Green(version.MassVersion())
fmt.Printf("🧰 CLI version: %v (git SHA: %v)\n", massVersionColor, version.MassGitSHA())

// Best-effort: check whether a newer CLI is available (does not affect exit code).
if latestVersion, err := version.GetLatestVersion(); err == nil {
if isOld, _ := version.CheckForNewerVersionAvailable(latestVersion); isOld {
fmt.Printf("⬆️ A newer version of the CLI is available, you can download it here: %v\n", version.LatestReleaseURL)
}
}

// Best-effort: if we can authenticate, show the Massdriver server version too.
ctx := context.Background()
mdClient, err := client.New()
if err != nil {
fmt.Printf("Could not check for newer version, skipping. url:%s error:%s", version.LatestReleaseURL, err.Error())
return
}
isOld, _ := version.CheckForNewerVersionAvailable(latestVersion)
if isOld {
fmt.Printf("A newer version of the CLI is available, you can download it here: %v\n", version.LatestReleaseURL)

if server, err := api.GetServer(ctx, mdClient); err == nil && server != nil && server.Version != "" {
fmt.Printf("🌐 Server version: %v\n", prettylogs.Green(server.Version))
}
massVersionColor := prettylogs.Green(version.MassVersion())
fmt.Printf("Mass CLI version: %v (git SHA: %v) \n", massVersionColor, version.MassGitSHA())
}
16 changes: 10 additions & 6 deletions pkg/api/cost.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package api

type Cost struct {
Monthly *CostType `json:"monthly"`
Daily *CostType `json:"daily"`
Monthly Summary `json:"monthly"`
Daily Summary `json:"daily"`
}

type CostType struct {
Average *CostSummary `json:"average"`
// Summary of costs over a time period.
type Summary struct {
Previous CostSample `json:"previous"`
Average CostSample `json:"average"`
}

type CostSummary struct {
Amount float64 `json:"amount"`
// A single cost measurement. Fields may be null when no cost data exists.
type CostSample struct {
Amount *float64 `json:"amount"`
Currency *string `json:"currency"`
}
2 changes: 1 addition & 1 deletion pkg/api/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Environment struct {
Name string `json:"name"`
Slug string `json:"slug"`
Description string `json:"description,omitempty"`
Cost *Cost `json:"cost,omitempty" mapstructure:"cost,omitempty"`
Cost Cost `json:"cost" mapstructure:"cost"`
Packages []Package `json:"packages,omitempty" mapstructure:"packages,omitempty"`
Project *Project `json:"project,omitempty" mapstructure:"project,omitempty"`
}
Expand Down
44 changes: 23 additions & 21 deletions pkg/api/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@ import (
"github.com/massdriver-cloud/massdriver-sdk-go/massdriver/client"
)

func floatPtr(v float64) *float64 { return &v }

func TestGetEnvironment(t *testing.T) {
want := api.Environment{
ID: "env-uuid1",
Name: "Test Environment",
Slug: "env",
Description: "This is a test environment",
Cost: &api.Cost{
Daily: &api.CostType{
Average: &api.CostSummary{
Amount: 10.0,
Cost: api.Cost{
Daily: api.Summary{
Average: api.CostSample{
Amount: floatPtr(10.0),
},
},
Monthly: &api.CostType{
Average: &api.CostSummary{
Amount: 300.0,
Monthly: api.Summary{
Average: api.CostSample{
Amount: floatPtr(300.0),
},
},
},
Expand Down Expand Up @@ -150,15 +152,15 @@ func TestGetEnvironmentsByPackage(t *testing.T) {
Name: "Test Environment 1",
Slug: "env1",
Description: "First test environment",
Cost: &api.Cost{
Daily: &api.CostType{
Average: &api.CostSummary{
Amount: 5.0,
Cost: api.Cost{
Daily: api.Summary{
Average: api.CostSample{
Amount: floatPtr(5.0),
},
},
Monthly: &api.CostType{
Average: &api.CostSummary{
Amount: 150.0,
Monthly: api.Summary{
Average: api.CostSample{
Amount: floatPtr(150.0),
},
},
},
Expand All @@ -172,15 +174,15 @@ func TestGetEnvironmentsByPackage(t *testing.T) {
Name: "Test Environment 2",
Slug: "env2",
Description: "Second test environment",
Cost: &api.Cost{
Daily: &api.CostType{
Average: &api.CostSummary{
Amount: 8.0,
Cost: api.Cost{
Daily: api.Summary{
Average: api.CostSample{
Amount: floatPtr(8.0),
},
},
Monthly: &api.CostType{
Average: &api.CostSummary{
Amount: 240.0,
Monthly: api.Summary{
Average: api.CostSample{
Amount: floatPtr(240.0),
},
},
},
Expand Down
6 changes: 6 additions & 0 deletions pkg/api/genqlient.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,13 @@ query getEnvironmentById($organizationId: ID!, $id: ID!) {
cost {
monthly {
average {
# @genqlient(pointer: true)
amount
}
}
daily {
average {
# @genqlient(pointer: true)
amount
}
}
Expand Down Expand Up @@ -277,11 +279,13 @@ query getEnvironmentsByProject($organizationId: ID!, $projectId: ID!) {
cost {
monthly {
average {
# @genqlient(pointer: true)
amount
}
}
daily {
average {
# @genqlient(pointer: true)
amount
}
}
Expand Down Expand Up @@ -528,11 +532,13 @@ query getProjects($organizationId: ID!){
cost{
monthly{
average{
# @genqlient(pointer: true)
amount
}
}
daily{
average{
# @genqlient(pointer: true)
amount
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Project struct {
Slug string `json:"slug"`
Description string `json:"description"`
DefaultParams map[string]any `json:"defaultParams"`
Cost *Cost `json:"cost,omitempty"`
Cost Cost `json:"cost"`
Environments []Environment `json:"environments"`
}

Expand Down
Loading
Loading