Skip to content

Commit

Permalink
feat: cli refactor (#682)
Browse files Browse the repository at this point in the history
  • Loading branch information
DOOduneye authored Apr 28, 2024
1 parent 74eb4ec commit 91fac39
Show file tree
Hide file tree
Showing 56 changed files with 1,356 additions and 1,172 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
- If you get an error about `expo-cli` not being installed, run `yarn global add expo-cli` and then run `yarn install` again.

```console
cd server
cd backend
go get ./...
```

Expand Down Expand Up @@ -119,7 +119,7 @@ To create a new migration, you can use the CLI

### SAC CLI

To install use `./scripts/install_cli.sh` and then run `sac` to see all commands.
To install use `./install_cli.sh` and then run `sac` to see all commands.

# Git Flow

Expand Down
2 changes: 1 addition & 1 deletion cli/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ linters:

linters-settings:
whitespace:
multi-func: true
multi-func: true
81 changes: 81 additions & 0 deletions cli/cmd/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package cmd

import (
"fmt"
"os"

"github.com/GenerateNU/sac/cli/helpers"
"github.com/spf13/cobra"
)

var dbCmd = &cobra.Command{
Use: "database",
Aliases: []string{"db"},
Short: "Database management commands",
}

func init() {
rootCmd.AddCommand(dbCmd)
dbCmd.AddCommand(dbInitCmd)
dbCmd.AddCommand(dbDownCmd)
dbCmd.AddCommand(dbResetCmd)
dbCmd.AddCommand(dbInsertCmd)
}

var dbInitCmd = &cobra.Command{
Use: "init",
Short: "Initialize the database",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
err := helpers.InitDB()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}

var dbDownCmd = &cobra.Command{
Use: "down",
Short: "Migrate down the database",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
err := helpers.DownDB()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}

var dbResetCmd = &cobra.Command{
Use: "reset",
Short: "Reset the database",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
err := helpers.DownDB()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

err = helpers.InitDB()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}

var dbInsertCmd = &cobra.Command{
Use: "insert",
Short: "Insert mock data into the database",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
err := helpers.InsertDB()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}
131 changes: 131 additions & 0 deletions cli/cmd/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package cmd

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

"github.com/GenerateNU/sac/cli/helpers"
"github.com/spf13/cobra"
)

var formatCmd = &cobra.Command{
Use: "format",
Aliases: []string{"f"},
Short: "Formatting commands",
}

func init() {
rootCmd.AddCommand(formatCmd)
formatCmd.AddCommand(formatFrontendCmd)
formatCmd.AddCommand(formatBackendCmd)
formatCmd.AddCommand(formatCliCmd)

formatFrontendCmd.AddCommand(formatWebCmd)
formatFrontendCmd.AddCommand(formatMobileCmd)
formatFrontendCmd.AddCommand(formatDashboardCmd)
formatFrontendCmd.AddCommand(formatLibCmd)
}

var formatFrontendCmd = &cobra.Command{
Use: "frontend",
Aliases: []string{"fe"},
Short: "Frontend formatting commands",
}

var formatWebCmd = &cobra.Command{
Use: "web",
Aliases: []string{"w"},
Short: "Frontend web formatting commands",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
err := helpers.Execute(exec.Command("yarn", "run", "format"), helpers.WEB_DIR)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}

var formatMobileCmd = &cobra.Command{
Use: "mobile",
Aliases: []string{"m"},
Short: "Formats the frontend mobile",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
err := helpers.Execute(exec.Command("yarn", "run", "format"), helpers.MOBILE_DIR)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}

var formatDashboardCmd = &cobra.Command{
Use: "dashboard",
Aliases: []string{"d"},
Short: "Formats the frontend dashboard",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
err := helpers.Execute(exec.Command("yarn", "run", "format"), helpers.DASHBOARD_DIR)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}

var formatLibCmd = &cobra.Command{
Use: "lib",
Aliases: []string{"l"},
Short: "Formats the frontend lib",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
err := helpers.Execute(exec.Command("yarn", "run", "format"), helpers.LIB_DIR)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}

var formatBackendCmd = &cobra.Command{
Use: "backend",
Short: "Formats the backend",
Aliases: []string{"be"},
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
_, err := exec.LookPath("gofumpt")
if err != nil {
fmt.Println("gofumpt is not installed. Please run the following command to install it:")
fmt.Println("go install mvdan.cc/gofumpt@latest")
os.Exit(1)
}

err = helpers.Execute(exec.Command("gofumpt", "-l", "-w", "."), helpers.BACKEND_DIR)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}

var formatCliCmd = &cobra.Command{
Use: "cli",
Short: "Formats the cli",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
_, err := exec.LookPath("gofumpt")
if err != nil {
fmt.Println("gofumpt is not installed. Please run the following command to install it:")
fmt.Println("go install mvdan.cc/gofumpt@latest")
os.Exit(1)
}

err = helpers.Execute(exec.Command("gofumpt", "-l", "-w", "."), helpers.CLI_DIR)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}
131 changes: 131 additions & 0 deletions cli/cmd/lint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package cmd

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

"github.com/GenerateNU/sac/cli/helpers"
"github.com/spf13/cobra"
)

var lintCmd = &cobra.Command{
Use: "lint",
Aliases: []string{"l"},
Short: "Linting commands",
}

func init() {
rootCmd.AddCommand(lintCmd)
lintCmd.AddCommand(lintFrontendCmd)
lintCmd.AddCommand(lintBackendCmd)
lintCmd.AddCommand(lintCliCmd)

lintFrontendCmd.AddCommand(lintWebCmd)
lintFrontendCmd.AddCommand(lintMobileCmd)
lintFrontendCmd.AddCommand(lintDashboardCmd)
lintFrontendCmd.AddCommand(lintLibCmd)
}

var lintFrontendCmd = &cobra.Command{
Use: "frontend",
Aliases: []string{"fe"},
Short: "Frontend linting commands",
}

var lintWebCmd = &cobra.Command{
Use: "web",
Short: "Lints the frontend web",
Aliases: []string{"w"},
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
err := helpers.Execute(exec.Command("yarn", "run", "lint", "--fix"), helpers.WEB_DIR)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}

var lintMobileCmd = &cobra.Command{
Use: "mobile",
Short: "Lints the frontend mobile",
Aliases: []string{"m"},
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
err := helpers.Execute(exec.Command("yarn", "run", "lint", "--fix"), helpers.MOBILE_DIR)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}

var lintDashboardCmd = &cobra.Command{
Use: "dashboard",
Short: "Lints the frontend dashboard",
Aliases: []string{"d"},
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
err := helpers.Execute(exec.Command("yarn", "run", "lint", "--fix"), helpers.DASHBOARD_DIR)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}

var lintLibCmd = &cobra.Command{
Use: "lib",
Short: "Lints the lib directory",
Aliases: []string{"l"},
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
err := helpers.Execute(exec.Command("yarn", "run", "lint", "--fix"), helpers.LIB_DIR)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}

var lintBackendCmd = &cobra.Command{
Use: "backend",
Short: "Lints the backend",
Aliases: []string{"be"},
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
_, err := exec.LookPath("golangci-lint")
if err != nil {
fmt.Println("golangci-lint is not installed. Please install it by running the following command:")
fmt.Println("curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.57.2")
os.Exit(1)
}

err = helpers.Execute(exec.Command("golangci-lint", "run", "--fix"), helpers.BACKEND_DIR)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}

var lintCliCmd = &cobra.Command{
Use: "cli",
Short: "Lints the CLI",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
_, err := exec.LookPath("golangci-lint")
if err != nil {
fmt.Println("golangci-lint is not installed. Please install it by running the following command:")
fmt.Println("curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.57.2")
os.Exit(1)
}

err = helpers.Execute(exec.Command("golangci-lint", "run", "--fix"), helpers.CLI_DIR)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}
28 changes: 28 additions & 0 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"os"

"github.com/spf13/cobra"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "sac",
Short: "SAC manages the GenerateNU Student Activity Calendar platform",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
Loading

0 comments on commit 91fac39

Please sign in to comment.