From 80403d034fffcfe3060af8cb463a65fc18f7700c Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Wed, 31 Jan 2024 06:40:36 -0800 Subject: [PATCH] Add command for getting current account --- cmd/replicate/main.go | 3 ++ go.mod | 2 +- go.sum | 4 +- go.work.sum | 10 ++--- internal/cmd/account.go | 18 +++++++++ internal/cmd/account/current.go | 71 +++++++++++++++++++++++++++++++++ internal/cmd/account/root.go | 24 +++++++++++ 7 files changed, 122 insertions(+), 10 deletions(-) create mode 100644 internal/cmd/account.go create mode 100644 internal/cmd/account/current.go create mode 100644 internal/cmd/account/root.go diff --git a/cmd/replicate/main.go b/cmd/replicate/main.go index f0aea84..2d6860e 100644 --- a/cmd/replicate/main.go +++ b/cmd/replicate/main.go @@ -7,6 +7,7 @@ import ( "github.com/replicate/cli/internal" "github.com/replicate/cli/internal/cmd" + "github.com/replicate/cli/internal/cmd/account" "github.com/replicate/cli/internal/cmd/auth" "github.com/replicate/cli/internal/cmd/hardware" "github.com/replicate/cli/internal/cmd/model" @@ -35,6 +36,7 @@ func init() { Title: "Core commands:", }) for _, cmd := range []*cobra.Command{ + account.RootCmd, auth.RootCmd, model.RootCmd, prediction.RootCmd, @@ -54,6 +56,7 @@ func init() { cmd.RunCmd, cmd.TrainCmd, cmd.StreamCmd, + cmd.AccountCmd, } { rootCmd.AddCommand(cmd) cmd.GroupID = "alias" diff --git a/go.mod b/go.mod index 3be94c3..35b57a5 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( github.com/getkin/kin-openapi v0.123.0 github.com/golangci/golangci-lint v1.55.2 github.com/mattn/go-isatty v0.0.20 - github.com/replicate/replicate-go v0.14.2 + github.com/replicate/replicate-go v0.15.0 github.com/schollz/progressbar/v3 v3.13.1 github.com/spf13/cobra v1.8.0 github.com/stretchr/testify v1.8.4 diff --git a/go.sum b/go.sum index 1c9fb46..85990f1 100644 --- a/go.sum +++ b/go.sum @@ -522,8 +522,8 @@ github.com/quasilyte/regex/syntax v0.0.0-20210819130434-b3f0c404a727 h1:TCg2WBOl github.com/quasilyte/regex/syntax v0.0.0-20210819130434-b3f0c404a727/go.mod h1:rlzQ04UMyJXu/aOvhd8qT+hvDrFpiwqp8MRXDY9szc0= github.com/quasilyte/stdinfo v0.0.0-20220114132959-f7386bf02567 h1:M8mH9eK4OUR4lu7Gd+PU1fV2/qnDNfzT635KRSObncs= github.com/quasilyte/stdinfo v0.0.0-20220114132959-f7386bf02567/go.mod h1:DWNGW8A4Y+GyBgPuaQJuWiy0XYftx4Xm/y5Jqk9I6VQ= -github.com/replicate/replicate-go v0.14.2 h1:XgK+REvYrWs7qDeyugxHA93h31qBhEFk/3p1/p2w3W8= -github.com/replicate/replicate-go v0.14.2/go.mod h1:otIrl1vDmyjNhTzmVmp/mQU3Wt1+3387gFNEsAZq0ig= +github.com/replicate/replicate-go v0.15.0 h1:irjAv6PI9H2o4H3sKkaHuQ35mKNC/0OQqNleYWNA4Sg= +github.com/replicate/replicate-go v0.15.0/go.mod h1:otIrl1vDmyjNhTzmVmp/mQU3Wt1+3387gFNEsAZq0ig= github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= diff --git a/go.work.sum b/go.work.sum index f8ec65c..a45efa5 100644 --- a/go.work.sum +++ b/go.work.sum @@ -15,6 +15,7 @@ github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+ github.com/googleapis/gax-go/v2 v2.4.0/go.mod h1:XOTVJ59hdnfJLIP/dh8n5CGryZR2LxK9wbMD5+iXC6c= github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= github.com/hashicorp/consul/api v1.12.0/go.mod h1:6pVBMo0ebnYdt2S3H87XhekM/HHrUoTD2XXb/VrZVy0= github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= github.com/hashicorp/go-hclog v1.2.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ= @@ -48,6 +49,8 @@ github.com/replicate/replicate-go v0.8.1 h1:Mza5hWR/R1akZRKwXtA/CQJ2pY4/B9fSCYX+ github.com/replicate/replicate-go v0.8.1/go.mod h1:k9C4+PaYa9+hMRjn4D7ZPHOCUFb8P4jhytsCqcGa2vU= github.com/replicate/replicate-go v0.13.0 h1:DWpSw8ck+dVK79jcVbg0iWJt4/ajcDaYX7FmiqSh2iI= github.com/replicate/replicate-go v0.13.0/go.mod h1:k9C4+PaYa9+hMRjn4D7ZPHOCUFb8P4jhytsCqcGa2vU= +github.com/replicate/replicate-go v0.15.0 h1:irjAv6PI9H2o4H3sKkaHuQ35mKNC/0OQqNleYWNA4Sg= +github.com/replicate/replicate-go v0.15.0/go.mod h1:otIrl1vDmyjNhTzmVmp/mQU3Wt1+3387gFNEsAZq0ig= github.com/sagikazarmark/crypt v0.6.0/go.mod h1:U8+INwJo3nBv1m6A/8OBXAq7Jnpspk5AxSgDyEQcea8= github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y= github.com/shirou/gopsutil/v3 v3.23.10/go.mod h1:JIE26kpucQi+innVlAUnIEOSBhBUkirr5b44yr55+WE= @@ -63,14 +66,7 @@ go.etcd.io/etcd/client/pkg/v3 v3.5.4/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3 go.etcd.io/etcd/client/v2 v2.305.4/go.mod h1:Ud+VUwIi9/uQHOMA+4ekToJ12lTxlv0zB/+DHwTGEbU= go.etcd.io/etcd/client/v3 v3.5.4/go.mod h1:ZaRkVgBZC+L+dLCjTcF1hRXpgZXQPOvnA/Ak/gq3kiY= go.opencensus.io v0.23.0/go.mod h1:XItmlyltB5F7CS4xOC1DcqMoFqwtC6OG2xF7mCv7P7E= -go.uber.org/nilaway v0.0.0-20231130193605-0ef3071d1630 h1:d78bN/STgxmCT0mDg+ZgR9EB3Z/lKq2i34sCvw39eMQ= golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= -golang.org/x/exp v0.0.0-20231108232855-2478ac86f678 h1:mchzmB1XO2pMaKFRqk/+MV3mgGG96aqaPXaMifQU47w= golang.org/x/lint v0.0.0-20210508222113-6edffad5e616/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= -golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= -golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg= -golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ= -golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= -golang.org/x/tools v0.15.0 h1:zdAyfUGbYmuVokhzVmghFl2ZJh5QhcfebBgmVPFYA+8= google.golang.org/api v0.81.0/go.mod h1:FA6Mb/bZxj706H2j+j2d6mHEEaHBmbbWnkfvmorOCko= google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd/go.mod h1:RAyBrSAP7Fh3Nc84ghnVLDPuV51xc9agzmm4Ph6i0Q4= diff --git a/internal/cmd/account.go b/internal/cmd/account.go new file mode 100644 index 0000000..423c388 --- /dev/null +++ b/internal/cmd/account.go @@ -0,0 +1,18 @@ +package cmd + +import ( + "github.com/spf13/cobra" + + "github.com/replicate/cli/internal/cmd/account" +) + +var AccountCmd = &cobra.Command{ + Use: "account", + Short: `Alias for "accounts current"`, + Aliases: []string{"profile", "whoami"}, + RunE: account.CurrentCmd.RunE, +} + +func init() { + account.AddCurrentAccountFlags(AccountCmd) +} diff --git a/internal/cmd/account/current.go b/internal/cmd/account/current.go new file mode 100644 index 0000000..49a9da5 --- /dev/null +++ b/internal/cmd/account/current.go @@ -0,0 +1,71 @@ +package account + +import ( + "encoding/json" + "fmt" + + "github.com/cli/browser" + "github.com/spf13/cobra" + + "github.com/replicate/cli/internal/client" + "github.com/replicate/cli/internal/util" +) + +// CurrentCmd represents the get current account command +var CurrentCmd = &cobra.Command{ + Use: "current", + Short: "Show the current account", + RunE: func(cmd *cobra.Command, args []string) error { + ctx := cmd.Context() + + r8, err := client.NewClient() + if err != nil { + return err + } + + account, err := r8.GetCurrentAccount(ctx) + if err != nil { + return fmt.Errorf("failed to get account: %w", err) + } + + if cmd.Flags().Changed("web") { + if util.IsTTY() { + fmt.Println("Opening in browser...") + } + + url := "https://replicate.com/" + account.Username + err := browser.OpenURL(url) + if err != nil { + return fmt.Errorf("failed to open browser: %w", err) + } + + return nil + } + + if cmd.Flags().Changed("json") || !util.IsTTY() { + bytes, err := json.MarshalIndent(account, "", " ") + if err != nil { + return fmt.Errorf("failed to marshal account: %w", err) + } + fmt.Println(string(bytes)) + return nil + } + + fmt.Printf("Type: %s\n", account.Type) + fmt.Printf("Username: %s\n", account.Username) + fmt.Printf("Name: %s\n", account.Name) + fmt.Printf("GitHub URL: %s\n", account.GithubURL) + + return nil + }, +} + +func init() { + AddCurrentAccountFlags(CurrentCmd) +} + +func AddCurrentAccountFlags(cmd *cobra.Command) { + cmd.Flags().Bool("json", false, "Emit JSON") + cmd.Flags().Bool("web", false, "View on web") + cmd.MarkFlagsMutuallyExclusive("json", "web") +} diff --git a/internal/cmd/account/root.go b/internal/cmd/account/root.go new file mode 100644 index 0000000..1473a9c --- /dev/null +++ b/internal/cmd/account/root.go @@ -0,0 +1,24 @@ +package account + +import ( + "github.com/spf13/cobra" +) + +var RootCmd = &cobra.Command{ + Use: "account [subcommand]", + Short: "Interact with accounts", + Aliases: []string{"accounts", "a"}, +} + +func init() { + RootCmd.AddGroup(&cobra.Group{ + ID: "subcommand", + Title: "Subcommands:", + }) + for _, cmd := range []*cobra.Command{ + CurrentCmd, + } { + RootCmd.AddCommand(cmd) + cmd.GroupID = "subcommand" + } +}