Skip to content

Commit

Permalink
Add disks command.
Browse files Browse the repository at this point in the history
  • Loading branch information
beornf committed Sep 4, 2024
1 parent f86ad86 commit 21eb9c6
Show file tree
Hide file tree
Showing 11 changed files with 305 additions and 34 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # pin@v3
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # pin@v4
with:
fetch-depth: 0
- name: Install Go
uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 # pin@v3
uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # pin@v5
with:
go-version: 1.18
go-version: 1.22
- name: Vet
run: go vet ./...
release:
Expand All @@ -22,22 +22,22 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # pin@v3
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # pin@v4
with:
fetch-depth: 0
- name: Install Go
uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 # pin@v3
uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # pin@v5
with:
go-version: 1.18
go-version: 1.22
- name: Generate Token
id: generate-token
uses: tibdex/github-app-token@021a2405c7f990db57f5eae5397423dcc554159c # pin@v1
uses: tibdex/github-app-token@3beb63f4bd073e61482598c45c71c1019b59b73a # pin@v2
with:
app_id: ${{ secrets.APP_ID }}
private_key: ${{ secrets.PRIVATE_KEY }}
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@b508e2e3ef3b19d4e4146d4f8fb3ba9db644a757 # pin@v3
uses: goreleaser/goreleaser-action@286f3b13b1b49da4ac219696163fb8c1c93e1200 # pin@v6
with:
args: release --rm-dist
args: release --clean
env:
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
7 changes: 4 additions & 3 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
version: 2
before:
hooks:
- go mod download
Expand All @@ -21,16 +22,16 @@ archives:
files:
- none*
brews:
- tap:
- repository:
owner: lade-io
name: homebrew-tap
folder: Formula
directory: Formula
homepage: https://github.com/lade-io/lade
description: Developer tool to manage your apps
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ .Tag }}-next"
version_template: "{{ .Tag }}-next"
changelog:
sort: asc
filters:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Commands:
addons Manage addons
apps Manage apps
deploy Deploy an app
disks Manage disks
domains Manage domains
env Manage app environment
help Help about any command
Expand Down
10 changes: 9 additions & 1 deletion cmd/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,16 @@ func getAppName() string {
return filepath.Base(cwd)
}

func getDiskPlan(client *lade.Client) string {
plan, err := client.Plan.Default("disk")
if err != nil {
return ""
}
return plan.ID
}

func getPlan(client *lade.Client) string {
plan, err := client.Plan.Default()
plan, err := client.Plan.Default("")
if err != nil {
return ""
}
Expand Down
212 changes: 212 additions & 0 deletions cmd/disks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
package cmd

import (
"strconv"

"github.com/AlecAivazis/survey/v2"
"github.com/dustin/go-humanize"
"github.com/lade-io/go-lade"
"github.com/rodaine/table"
"github.com/spf13/cobra"
)

var disksCmd = &cobra.Command{
Use: "disks",
Short: "Manage disks",
}

var disksAddCmd = func() *cobra.Command {
var appName string
opts := &lade.DiskCreateOpts{}
cmd := &cobra.Command{
Use: "add <disk-name>",
Short: "Add a disk to an app",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := getClient()
if err != nil {
return err
}
if len(args) > 0 {
opts.Name = args[0]
}
return disksAddRun(client, opts, appName)
},
}
cmd.Flags().StringVarP(&appName, "app", "a", "", "App Name")
cmd.Flags().StringVar(&opts.Path, "path", "", "Path")
cmd.Flags().StringVarP(&opts.PlanID, "plan", "p", "", "Plan")
return cmd
}()

var disksListCmd = func() *cobra.Command {
var appName string
cmd := &cobra.Command{
Use: "list",
Short: "List disks of an app",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
client, err := getClient()
if err != nil {
return err
}
return disksListRun(client, appName)
},
}
cmd.Flags().StringVarP(&appName, "app", "a", "", "App Name")
return cmd
}()

var disksPlansCmd = &cobra.Command{
Use: "plans",
Short: "List available plans",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
client, err := getClient()
if err != nil {
return err
}
return disksPlansRun(client)
},
}

var disksRemoveCmd = func() *cobra.Command {
var appName string
cmd := &cobra.Command{
Use: "remove <disk-name>",
Short: "Remove a disk from an app",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := getClient()
if err != nil {
return err
}
var diskName string
if len(args) > 0 {
diskName = args[0]
}
return disksRemoveRun(client, appName, diskName)
},
}
cmd.Flags().StringVarP(&appName, "app", "a", "", "App Name")
return cmd
}()

var disksUpdateCmd = func() *cobra.Command {
var appName string
opts := &lade.DiskUpdateOpts{}
cmd := &cobra.Command{
Use: "update <disk-name>",
Short: "Update a disk of an app",
RunE: func(cmd *cobra.Command, args []string) error {
client, err := getClient()
if err != nil {
return err
}
var diskName string
if len(args) > 0 {
diskName = args[0]
}
return disksUpdateRun(client, opts, appName, diskName)
},
}
cmd.Flags().StringVarP(&appName, "app", "a", "", "App Name")
cmd.Flags().StringVarP(&opts.PlanID, "plan", "p", "", "Plan")
return cmd
}()

func init() {
disksCmd.AddCommand(disksAddCmd)
disksCmd.AddCommand(disksListCmd)
disksCmd.AddCommand(disksPlansCmd)
disksCmd.AddCommand(disksRemoveCmd)
disksCmd.AddCommand(disksUpdateCmd)
}

func disksAddRun(client *lade.Client, opts *lade.DiskCreateOpts, appName string) error {
if err := askSelect("App Name:", getAppName, client, getAppOptions, &appName); err != nil {
return err
}
if err := askInput("Disk Name:", appName, &opts.Name, validateDiskName(client, appName)); err != nil {
return err
}
if err := askSelect("Plan:", getDiskPlan, client, getDiskPlanOptions(""), &opts.PlanID); err != nil {
return err
}
if err := askInput("Path:", "/data", &opts.Path, validatePath); err != nil {
return err
}
_, err := client.Disk.Create(appName, opts)
return err
}

func disksListRun(client *lade.Client, appName string) error {
if err := askSelect("App Name:", getAppName, client, getAppOptions, &appName); err != nil {
return err
}
disks, err := client.Disk.List(appName)
if err != nil {
return err
}
t := table.New("NAME", "PLAN", "PATH", "CREATED")
for _, disk := range disks {
t.AddRow(disk.Name, disk.PlanID, disk.Path, humanize.Time(disk.CreatedAt))
}
t.Print()
return nil
}

func disksPlansRun(client *lade.Client) error {
plans, err := client.Plan.List("disk")
if err != nil {
return err
}
t := table.New("ID", "DISK", "PRICE HOURLY", "PRICE MONTHLY")
for _, plan := range plans {
priceHourly := printPrice(plan.PriceHourly, -1)
priceMonthly := printPrice(plan.PriceMonthly, 2)
t.AddRow(plan.ID, plan.Disk, priceHourly, priceMonthly)
}
t.Print()
return nil
}

func disksRemoveRun(client *lade.Client, appName, diskName string) error {
if err := askSelect("App Name:", getAppName, client, getAppOptions, &appName); err != nil {
return err
}
if err := askSelect("Disk Name:", "", client, getDiskOptions(appName), &diskName); err != nil {
return err
}
disk, err := client.Disk.Get(appName, diskName)
if err != nil {
return err
}
prompt := &survey.Confirm{
Message: "Do you really want to delete " + disk.Name + "?",
}
confirm := false
survey.AskOne(prompt, &confirm, nil)
if confirm {
err = client.Disk.Delete(disk)
}
return err
}

func disksUpdateRun(client *lade.Client, opts *lade.DiskUpdateOpts, appName, diskName string) error {
if err := askSelect("App Name:", getAppName, client, getAppOptions, &appName); err != nil {
return err
}
if err := askSelect("Disk Name:", "", client, getDiskOptions(appName), &diskName); err != nil {
return err
}
disk, err := client.Disk.Get(appName, diskName)
if err != nil {
return err
}
if err = askSelect("Plan:", disk.PlanID, client, getDiskPlanOptions(disk.PlanID), &opts.PlanID); err != nil {
return err
}
_, err = client.Disk.Update(strconv.Itoa(disk.AppID), strconv.Itoa(disk.ID), opts)
return err
}
2 changes: 1 addition & 1 deletion cmd/plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var plansCmd = &cobra.Command{
}

func plansRun(client *lade.Client) error {
plans, err := client.Plan.List()
plans, err := client.Plan.List("")
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 21eb9c6

Please sign in to comment.