Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding the update functionality #34

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
29 changes: 29 additions & 0 deletions src/cli/cmd/future.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cmd

import (
"github.com/devhindo/x/src/cli/tweet"

"github.com/spf13/cobra"
)

var (
wait string
date string
time string
content string
)

func init() {
rootCmd.AddCommand(futureCmd)
}

var futureCmd = &cobra.Command{
Use: "-f",
Short: "Post future tweets",
Long: `Post future tweets.`,
Run: futureCmdRun,
}

func futureCmdRun(cmd *cobra.Command, args []string) {
tweet.PostFutureTweet(args)
}
45 changes: 45 additions & 0 deletions src/cli/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

var (
version = "1.1.3"
man = fmt.Sprintf(`interact with x (twitter) from terminal. version: %s.

USAGE
x <command>

Commands
-h show this help
auth start authorizing your X account
auth --url get auth url if it didn't open browser after running 'x auth'
auth -v verify authorization after running 'x auth'
-t "text" post a tweet
-v show version (%s)
-c clear authorized account`, version, version)
)

var rootCmd = &cobra.Command{
Use: "x",
Short: "x is a CLI tool for posting tweets",
Long: man,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(man)
},
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

func init() {

}
118 changes: 118 additions & 0 deletions src/cli/cmd/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package cmd

import (
"fmt"
"os"
"os/exec"
"regexp"

"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(updateCmd)

updateCmd.Flags().StringVarP(&vFlag, "version", "v", "latest", "Update the CLI to specific version")
}

var (
vFlag string

updateCmd = &cobra.Command{
Use: "update",
Short: "Update the CLI to specific version ",
Long: `Update the CLI to the latest version or specify a version using -v flag
Example: x update -v 1.1.1
default: x update -v latest
`,
RunE: update,
}
)

func update(cmd *cobra.Command, args []string) error {

fmt.Println("Updating CLI...")

vFlag, err := cmd.Flags().GetString("version")
if err != nil {
err = fmt.Errorf("error getting version flag: %v", err)
return err
}

// validate version
if vFlag != "latest" {
err = validateVersion(vFlag)
if err != nil {
return err
}
}

if isGoInstalled() {
err = updateUsingGo()
if err != nil {
return err
}
return nil
}

return nil
}

func validateVersion(v string) error {
// Regular expression pattern for semantic versioning
pattern := `^v\d+\.\d+\.\d+$`
match, err := regexp.MatchString(pattern, v)
if err != nil {
return err
}
if !match {
return fmt.Errorf("invalid version format. Please use semantic versioning like v1.1.1")
}
return nil
}

func cmdUpdate() {
if !isGoInstalled() {
fmt.Println("")
}

cmd := exec.Command("go", "get", "-u", "github.com/bradford-hamilton/monkey")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

if isGoInstalled() {
fmt.Println("Go is installed...")

return
}

fmt.Println("Go is not installed...")

fmt.Println("CLI updated successfully!")
}

func isGoInstalled() bool {
_, err := exec.LookPath("go")

return err == nil
}

func updateUsingGo() error {
cmd := exec.Command("go", "get", "-u", "github.com/devhindo/x/src/cli/cmd")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Println(err)
return err
}
fmt.Println("CLI updated successfully!")
return nil
}

//TODO: x update -v 1.1.1 (default for v:latest)
22 changes: 22 additions & 0 deletions src/cli/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(versionCmd)
}

//TODO: make it for -v and --version too

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version of the CLI",
Long: `Print the version of the CLI`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(version)
},
}
6 changes: 6 additions & 0 deletions src/cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ module github.com/devhindo/x/src/cli
go 1.22.2

require github.com/google/uuid v1.3.1

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/cobra v1.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
10 changes: 10 additions & 0 deletions src/cli/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
5 changes: 3 additions & 2 deletions src/cli/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

import "github.com/devhindo/x/src/cli/x"
import "github.com/devhindo/x/src/cli/cmd"


func main() {
x.Run()
//x.Run()
cmd.Execute()
}
7 changes: 6 additions & 1 deletion src/cli/x/run.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package x

import (
"github.com/devhindo/x/src/cli/cmd"
)

func Run() {
HandleArgs()
// HandleArgs()
cmd.Execute()
}
Loading