diff --git a/cli/.golangci.yml b/cli/.golangci.yml new file mode 100644 index 000000000..d1df6cbd3 --- /dev/null +++ b/cli/.golangci.yml @@ -0,0 +1,16 @@ +linters: + enable: + - exportloopref + - gocritic + - gosec + - ineffassign + - misspell + - prealloc + - unconvert + - unparam + - goimports + - whitespace + +linters-settings: + whitespace: + multi-func: true \ No newline at end of file diff --git a/cli/LICENSE b/cli/LICENSE deleted file mode 100644 index e69de29bb..000000000 diff --git a/cli/cmd/db.go b/cli/cmd/db.go index e3491dfef..001f6cc59 100644 --- a/cli/cmd/db.go +++ b/cli/cmd/db.go @@ -9,9 +9,9 @@ import ( ) var dbCmd = &cobra.Command{ - Use: "database", + Use: "database", Aliases: []string{"db"}, - Short: "Database management commands", + Short: "Database management commands", } func init() { @@ -78,4 +78,4 @@ var dbInsertCmd = &cobra.Command{ os.Exit(1) } }, -} \ No newline at end of file +} diff --git a/cli/cmd/format.go b/cli/cmd/format.go index f392b5122..788f5cf6a 100644 --- a/cli/cmd/format.go +++ b/cli/cmd/format.go @@ -10,15 +10,16 @@ import ( ) var formatCmd = &cobra.Command{ - Use: "format", + Use: "format", Aliases: []string{"f"}, - Short: "Executeing commands", + Short: "Formatting commands", } func init() { rootCmd.AddCommand(formatCmd) formatCmd.AddCommand(formatFrontendCmd) formatCmd.AddCommand(formatBackendCmd) + formatCmd.AddCommand(formatCliCmd) formatFrontendCmd.AddCommand(formatWebCmd) formatFrontendCmd.AddCommand(formatMobileCmd) @@ -27,16 +28,16 @@ func init() { } var formatFrontendCmd = &cobra.Command{ - Use: "frontend", + Use: "frontend", Aliases: []string{"fe"}, - Short: "Frontend formatting commands", + Short: "Frontend formatting commands", } var formatWebCmd = &cobra.Command{ - Use: "web", + Use: "web", Aliases: []string{"w"}, - Short: "Frontend web formatting commands", - Args: cobra.NoArgs, + 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 { @@ -47,10 +48,10 @@ var formatWebCmd = &cobra.Command{ } var formatMobileCmd = &cobra.Command{ - Use: "mobile", + Use: "mobile", Aliases: []string{"m"}, - Short: "Frontend mobile formatting commands", - Args: cobra.NoArgs, + 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 { @@ -61,10 +62,10 @@ var formatMobileCmd = &cobra.Command{ } var formatDashboardCmd = &cobra.Command{ - Use: "dashboard", + Use: "dashboard", Aliases: []string{"d"}, - Short: "Frontend dashboard formatting commands", - Args: cobra.NoArgs, + 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 { @@ -75,10 +76,10 @@ var formatDashboardCmd = &cobra.Command{ } var formatLibCmd = &cobra.Command{ - Use: "lib", + Use: "lib", Aliases: []string{"l"}, - Short: "Frontend lib formatting commands", - Args: cobra.NoArgs, + 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 { @@ -89,12 +90,39 @@ var formatLibCmd = &cobra.Command{ } var formatBackendCmd = &cobra.Command{ - Use: "backend", - Short: "Backend formatting commands", + 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 := helpers.Execute(exec.Command("go", "fmt", "./..."), helpers.BACKEND_DIR) + _, 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) diff --git a/cli/cmd/lint.go b/cli/cmd/lint.go index a95c62974..b97d87078 100644 --- a/cli/cmd/lint.go +++ b/cli/cmd/lint.go @@ -10,46 +10,36 @@ import ( ) var lintCmd = &cobra.Command{ - Use: "lint", + Use: "lint", Aliases: []string{"l"}, - Short: "Linting commands", + 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) - - lintWebCmd.Flags().BoolP("fix", "f", false, "Fix linting errors") - lintMobileCmd.Flags().BoolP("fix", "f", false, "Fix linting errors") - lintDashboardCmd.Flags().BoolP("fix", "f", false, "Fix linting errors") } var lintFrontendCmd = &cobra.Command{ - Use: "frontend", + Use: "frontend", Aliases: []string{"fe"}, - Short: "Frontend linting commands", + Short: "Frontend linting commands", } var lintWebCmd = &cobra.Command{ - Use: "web", - Short: "Frontend web linting commands", + Use: "web", + Short: "Lints the frontend web", Aliases: []string{"w"}, - Args: cobra.NoArgs, + Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { - fix, _ := cmd.Flags().GetBool("fix") - - var command string - if fix { - command = " --fix" - } - - err := helpers.Execute(exec.Command("yarn", "run", "lint"+command), helpers.WEB_DIR) + err := helpers.Execute(exec.Command("yarn", "run", "lint", "--fix"), helpers.WEB_DIR) if err != nil { fmt.Println(err) os.Exit(1) @@ -58,19 +48,12 @@ var lintWebCmd = &cobra.Command{ } var lintMobileCmd = &cobra.Command{ - Use: "mobile", - Short: "Frontend mobile linting commands", + Use: "mobile", + Short: "Lints the frontend mobile", Aliases: []string{"m"}, - Args: cobra.NoArgs, + Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { - fix, _ := cmd.Flags().GetBool("fix") - - var command string - if fix { - command = " --fix" - } - - err := helpers.Execute(exec.Command("yarn", "run", "lint"+command), helpers.MOBILE_DIR) + err := helpers.Execute(exec.Command("yarn", "run", "lint", "--fix"), helpers.MOBILE_DIR) if err != nil { fmt.Println(err) os.Exit(1) @@ -79,19 +62,12 @@ var lintMobileCmd = &cobra.Command{ } var lintDashboardCmd = &cobra.Command{ - Use: "dashboard", - Short: "Frontend dashboard linting commands", + Use: "dashboard", + Short: "Lints the frontend dashboard", Aliases: []string{"d"}, - Args: cobra.NoArgs, + Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { - fix, _ := cmd.Flags().GetBool("fix") - - var command string - if fix { - command = " --fix" - } - - err := helpers.Execute(exec.Command("yarn", "run", "lint"+command), helpers.DASHBOARD_DIR) + err := helpers.Execute(exec.Command("yarn", "run", "lint", "--fix"), helpers.DASHBOARD_DIR) if err != nil { fmt.Println(err) os.Exit(1) @@ -100,19 +76,33 @@ var lintDashboardCmd = &cobra.Command{ } var lintLibCmd = &cobra.Command{ - Use: "lib", - Short: "Frontend lib linting commands", + Use: "lib", + Short: "Lints the lib directory", Aliases: []string{"l"}, - Args: cobra.NoArgs, + Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { - fix, _ := cmd.Flags().GetBool("fix") + err := helpers.Execute(exec.Command("yarn", "run", "lint", "--fix"), helpers.LIB_DIR) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + }, +} - var command string - if fix { - command = " --fix" +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("yarn", "run", "lint"+command), helpers.LIB_DIR) + err = helpers.Execute(exec.Command("golangci-lint", "run", "--fix"), helpers.BACKEND_DIR) if err != nil { fmt.Println(err) os.Exit(1) @@ -120,14 +110,19 @@ var lintLibCmd = &cobra.Command{ }, } -var lintBackendCmd = &cobra.Command{ - Use: "backend", - Short: "Backend linting commands", - Aliases: []string{"be"}, +var lintCliCmd = &cobra.Command{ + Use: "cli", + Short: "Lints the CLI", Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { - // TODO: golangci-lint - err := helpers.Execute(exec.Command("go", "lint", "./..."), helpers.BACKEND_DIR) + _, 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) diff --git a/cli/cmd/root.go b/cli/cmd/root.go index 64f83a9ef..d8c82df54 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -14,8 +14,8 @@ 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() - }, + return cmd.Help() + }, } // Execute adds all child commands to the root command and sets flags appropriately. @@ -25,4 +25,4 @@ func Execute() { if err != nil { os.Exit(1) } -} \ No newline at end of file +} diff --git a/cli/cmd/run.go b/cli/cmd/run.go index a2082f3ec..a406a99b6 100644 --- a/cli/cmd/run.go +++ b/cli/cmd/run.go @@ -10,9 +10,9 @@ import ( ) var runCmd = &cobra.Command{ - Use: "run", + Use: "run", Aliases: []string{"r"}, - Short: "Run the backend or frontend", + Short: "Run commands", } func init() { @@ -29,10 +29,10 @@ func init() { } var runBeCmd = &cobra.Command{ - Use: "backend", - Short: "Run the backend", + Use: "backend", + Short: "Run the backend", Aliases: []string{"be"}, - Args: cobra.NoArgs, + Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { err := helpers.Execute(exec.Command("go", "run", "main.go"), helpers.BACKEND_DIR) if err != nil { @@ -43,16 +43,16 @@ var runBeCmd = &cobra.Command{ } var runFeCmd = &cobra.Command{ - Use: "frontend", - Short: "Run the frontend", + Use: "frontend", + Short: "Frontend run commands", Aliases: []string{"fe"}, } var runWebCmd = &cobra.Command{ - Use: "web", - Short: "Run the web frontend", + Use: "web", + Short: "Run the web frontend", Aliases: []string{"w"}, - Args: cobra.NoArgs, + Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { err := helpers.Execute(exec.Command("yarn", "run", "dev"), helpers.WEB_DIR) if err != nil { @@ -63,10 +63,10 @@ var runWebCmd = &cobra.Command{ } var runMobileCmd = &cobra.Command{ - Use: "mobile", - Short: "Run the mobile frontend", + Use: "mobile", + Short: "Run the mobile frontend", Aliases: []string{"m"}, - Args: cobra.NoArgs, + Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { platform, _ := cmd.Flags().GetString("platform") @@ -81,10 +81,10 @@ var runMobileCmd = &cobra.Command{ } var runDashboardCmd = &cobra.Command{ - Use: "dashboard", - Short: "Run the dashboard frontend", + Use: "dashboard", + Short: "Run the dashboard frontend", Aliases: []string{"d"}, - Args: cobra.NoArgs, + Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { err := helpers.Execute(exec.Command("yarn", "run", "dev"), helpers.DASHBOARD_DIR) if err != nil { @@ -92,4 +92,4 @@ var runDashboardCmd = &cobra.Command{ os.Exit(1) } }, -} \ No newline at end of file +} diff --git a/cli/cmd/setup.go b/cli/cmd/setup.go index f59d8222c..e4a44624b 100644 --- a/cli/cmd/setup.go +++ b/cli/cmd/setup.go @@ -8,7 +8,6 @@ import ( var setupCmd = &cobra.Command{ Use: "setup", - Aliases: []string{"s"}, Short: "Installs and sets up the project", Run: func(cmd *cobra.Command, args []string) { fmt.Println("Not implemented") @@ -17,4 +16,4 @@ var setupCmd = &cobra.Command{ func init() { rootCmd.AddCommand(setupCmd) -} \ No newline at end of file +} diff --git a/cli/cmd/swagger.go b/cli/cmd/swagger.go index 00e921bb7..9215c8ff2 100644 --- a/cli/cmd/swagger.go +++ b/cli/cmd/swagger.go @@ -10,8 +10,8 @@ import ( ) var swagCmd = &cobra.Command{ - Use: "swagger", - Short: "Run swagger init", + Use: "swagger", + Short: "Run swagger init", Aliases: []string{"swag"}, Run: func(cmd *cobra.Command, args []string) { err := helpers.Execute(exec.Command("swag", "init"), helpers.BACKEND_DIR) @@ -24,4 +24,4 @@ var swagCmd = &cobra.Command{ func init() { rootCmd.AddCommand(swagCmd) -} \ No newline at end of file +} diff --git a/cli/cmd/test.go b/cli/cmd/test.go index f73f70caf..ccf2d5b52 100644 --- a/cli/cmd/test.go +++ b/cli/cmd/test.go @@ -10,15 +10,16 @@ import ( ) var testCmd = &cobra.Command{ - Use: "test", + Use: "test", Aliases: []string{"t"}, - Short: "Testing commands", + Short: "Testing commands", } func init() { rootCmd.AddCommand(testCmd) testCmd.AddCommand(testFrontendCmd) testCmd.AddCommand(testBackendCmd) + testCmd.AddCommand(testCliCmd) testCmd.AddCommand(testCleanDatabaseCmd) testBackendCmd.Flags().BoolP("clean", "c", false, "Clean the test cache after running tests") @@ -30,16 +31,16 @@ func init() { } var testFrontendCmd = &cobra.Command{ - Use: "frontend", + Use: "frontend", Aliases: []string{"fe"}, - Short: "Frontend testing commands", + Short: "Frontend testing commands", } var testWebCmd = &cobra.Command{ - Use: "web", - Short: "Frontend web testing commands", + Use: "web", + Short: "Frontend web testing commands", Aliases: []string{"w"}, - Args: cobra.NoArgs, + Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { err := helpers.Execute(exec.Command("yarn", "run", "test"), helpers.WEB_DIR) if err != nil { @@ -50,10 +51,10 @@ var testWebCmd = &cobra.Command{ } var testMobileCmd = &cobra.Command{ - Use: "mobile", - Short: "Frontend mobile testing commands", + Use: "mobile", + Short: "Frontend mobile testing commands", Aliases: []string{"m"}, - Args: cobra.NoArgs, + Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { err := helpers.Execute(exec.Command("yarn", "run", "test"), helpers.MOBILE_DIR) if err != nil { @@ -64,10 +65,10 @@ var testMobileCmd = &cobra.Command{ } var testDashboardCmd = &cobra.Command{ - Use: "dashboard", - Short: "Frontend dashboard testing commands", + Use: "dashboard", + Short: "Frontend dashboard testing commands", Aliases: []string{"d"}, - Args: cobra.NoArgs, + Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { err := helpers.Execute(exec.Command("yarn", "run", "test"), helpers.DASHBOARD_DIR) if err != nil { @@ -78,10 +79,10 @@ var testDashboardCmd = &cobra.Command{ } var testLibCmd = &cobra.Command{ - Use: "lib", - Short: "Frontend lib testing commands", + Use: "lib", + Short: "Frontend lib testing commands", Aliases: []string{"l"}, - Args: cobra.NoArgs, + Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { err := helpers.Execute(exec.Command("yarn", "run", "test"), helpers.LIB_DIR) if err != nil { @@ -92,10 +93,10 @@ var testLibCmd = &cobra.Command{ } var testBackendCmd = &cobra.Command{ - Use: "backend", - Short: "Backend testing commands", + Use: "backend", + Short: "Backend testing commands", Aliases: []string{"be"}, - Args: cobra.NoArgs, + Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { clear, _ := cmd.Flags().GetBool("clean") @@ -114,12 +115,24 @@ var testBackendCmd = &cobra.Command{ }, } +var testCliCmd = &cobra.Command{ + Use: "cli", + Short: "CLI testing commands", + Args: cobra.NoArgs, + Run: func(cmd *cobra.Command, args []string) { + err := helpers.Execute(exec.Command("go", "test", "./..."), helpers.CLI_DIR) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + }, +} var testCleanDatabaseCmd = &cobra.Command{ - Use: "clean-db", - Short: "Remove databases used for testing", + Use: "clean-db", + Short: "Remove databases used for testing", Aliases: []string{"clean"}, - Args: cobra.NoArgs, + Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { err := helpers.CleanTestDBs() if err != nil { @@ -127,4 +140,4 @@ var testCleanDatabaseCmd = &cobra.Command{ os.Exit(1) } }, -} \ No newline at end of file +} diff --git a/cli/helpers/backend.go b/cli/helpers/backend.go index e7e0f3492..498042e83 100644 --- a/cli/helpers/backend.go +++ b/cli/helpers/backend.go @@ -6,14 +6,15 @@ import ( ) func RunBackend() error { - err := InitDB(); if err != nil { + err := InitDB() + if err != nil { return fmt.Errorf("error initializing database: %w", err) } - err = Execute(exec.Command("go", "run", "main.go"), BACKEND_DIR) + err = Execute(exec.Command("go", "run", "main.go"), BACKEND_DIR) if err != nil { return fmt.Errorf("error running backend: %w", err) } - + return nil } diff --git a/cli/helpers/config.go b/cli/helpers/config.go index 43196b19a..bdcb76d36 100644 --- a/cli/helpers/config.go +++ b/cli/helpers/config.go @@ -14,6 +14,7 @@ var ( LIB_DIR = filepath.Join(FRONTEND_DIR, "/lib") WEB_DIR = filepath.Join(FRONTEND_DIR, "/web") BACKEND_DIR = filepath.Join(ROOT_DIR, "/backend") + CLI_DIR = filepath.Join(ROOT_DIR, "/cli") CONFIG = filepath.Join(ROOT_DIR, "/config") MIGRATIONS = filepath.Join(BACKEND_DIR, "/migrations") MOCK_FILE = filepath.Join(BACKEND_DIR, "/mock/data.sql") diff --git a/cli/helpers/database.go b/cli/helpers/database.go index 762b5e981..85fe05c03 100644 --- a/cli/helpers/database.go +++ b/cli/helpers/database.go @@ -17,7 +17,7 @@ func InitDB() error { if err != nil { return fmt.Errorf("error initializing database: %w", err) } - + return nil } @@ -152,4 +152,4 @@ func InsertDB() error { } return nil -} \ No newline at end of file +} diff --git a/cli/helpers/execute.go b/cli/helpers/execute.go index c59f05735..7d43c71d4 100644 --- a/cli/helpers/execute.go +++ b/cli/helpers/execute.go @@ -17,4 +17,4 @@ func Execute(cmd *exec.Cmd, dir string) error { } return nil -} \ No newline at end of file +} diff --git a/cli/utils/path.go b/cli/utils/path.go index ef8f27a08..83142047c 100644 --- a/cli/utils/path.go +++ b/cli/utils/path.go @@ -6,7 +6,6 @@ import ( "path/filepath" ) - func GetRootDir() (string, error) { // Get the current working directory currentDir, err := os.Getwd() @@ -29,7 +28,7 @@ func FindRootDir(dir string) (string, error) { _, err := os.Stat(mainGoPath) if err == nil { // "sac" found, this is the root directory - return dir, nil + return filepath.Join(dir, "sac"), nil } // If not found, go up one level diff --git a/install_cli.sh b/install_cli.sh index b3d6cecf9..6fd2ea5e3 100755 --- a/install_cli.sh +++ b/install_cli.sh @@ -8,9 +8,18 @@ SCRIPT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" cd "$SCRIPT_PATH/cli" +# Check for Windows or Unix-like system +if [[ $(uname -s) == "MINGW64" || $(uname -s) == "CYGWIN" || $(uname -s) == "MSYS" ]]; then + export GOPATH="%USERPROFILE%\go" +else + export GOPATH="$HOME/go" +fi + +# Build the executable go build -o "sac" -go install -rm sac +mkdir -p "$GOPATH/bin" + +mv "sac" "$GOPATH/bin" -echo "Installation complete. You can now run 'sac' from anywhere." +echo "Installation complete. You can now run 'sac'"