Skip to content

Commit

Permalink
updating root cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
lord-skinner committed Dec 20, 2024
1 parent a0fb873 commit 021c18a
Show file tree
Hide file tree
Showing 3 changed files with 965 additions and 6 deletions.
76 changes: 71 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,88 @@ package cmd

import (
"dg/style"
"fmt"
"os"
"runtime"

getter "github.com/hashicorp/go-getter"
"github.com/spf13/cobra"
)

// https://github.com/spf13/cobra/blob/main/README.md
// Define the tool version
var version = "v0.1.0"

var rootCmd = &cobra.Command{
Use: "dg",
Short: style.Dg.Render("\ndg is a cli written in GO to help improve the DX of Analytics Engineers using dbt"),
Long: style.Dg.Render("dg is a cli written in GO to help improve the DX of Analytics Engineers using dbt"),
RunE: executeRoot,
}

func init() {
rootCmd.PersistentFlags().BoolP("version", "v", false, "Print the version number of the CLI tool")
rootCmd.Flags().BoolP("update", "u", false, "Update the CLI tool to the latest version or a specified version")
}

func executeRoot(cmd *cobra.Command, args []string) error {
if cmd.Flag("version").Changed {
fmt.Printf("Current Version: %s\n", version)
return nil
}

if cmd.Flag("update").Changed {
var updateVersion string
if len(args) > 0 {
updateVersion = args[0]
}
return runUpdate(updateVersion)
}

cmd.Help()
return nil
}

func Execute() {
err := rootCmd.Execute()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}

var rootCmd = &cobra.Command{
Use: style.Dg.Render("dg"),
Short: style.Dg.Render("\ndg is a cli written in GO to help improve the DX of Analytics Engineers using dbt"),
Long: style.Dg.Render("dg is a cli written in GO to help improve the DX of Analytics Engineers using dbt"),
func runUpdate(version string) error {
versionParam := "latest/download/dg"

if version != "" {
versionParam = fmt.Sprintf("download/%s/dg", version)
}

url := fmt.Sprintf("https://github.com/cognite-analytics/dbt-go/releases/%s", versionParam)

fmt.Printf("Updating CLI tool from %s...\n", url)

tempFile := "/tmp/dg"
if runtime.GOOS == "windows" {
tempFile = "dg.exe"
}

err := getter.GetFile(tempFile, url)
if err != nil {
return fmt.Errorf("failed to download file: %v", err)
}

err = os.Rename(tempFile, os.Args[0])
if err != nil {
return fmt.Errorf("failed to replace the binary: %v", err)
}

if runtime.GOOS != "windows" {
err = os.Chmod(os.Args[0], 0755)
if err != nil {
return fmt.Errorf("failed to change the file permissions: %v", err)
}
}

fmt.Println("Update successful!")
return nil
}
32 changes: 31 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,59 @@ require (
)

require (
cloud.google.com/go v0.104.0 // indirect
cloud.google.com/go/compute v1.10.0 // indirect
cloud.google.com/go/iam v0.5.0 // indirect
cloud.google.com/go/storage v1.27.0 // indirect
github.com/aws/aws-sdk-go v1.44.122 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/charmbracelet/x/ansi v0.4.5 // indirect
github.com/charmbracelet/x/term v0.2.1 // indirect
github.com/disintegration/imaging v1.6.2 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/fogleman/gg v1.3.0 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.0 // indirect
github.com/googleapis/gax-go/v2 v2.6.0 // indirect
github.com/gookit/color v1.4.2 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-getter v1.7.6 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/klauspost/compress v1.15.11 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/makeworld-the-better-one/dither/v2 v2.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/nathan-fiscaletti/consolesize-go v0.0.0-20210105204122-a87d9f614b9d // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d // indirect
golang.org/x/net v0.1.0 // indirect
golang.org/x/oauth2 v0.1.0 // indirect
golang.org/x/sync v0.9.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.3.8 // indirect
golang.org/x/text v0.4.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.100.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20221025140454-527a21cfbd71 // indirect
google.golang.org/grpc v1.50.1 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)
Loading

0 comments on commit 021c18a

Please sign in to comment.