-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #405 from jovandeginste/add-debug-cmd
Add a debugger tool
- Loading branch information
Showing
114 changed files
with
29,902 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package main | ||
|
||
import ( | ||
appassets "github.com/jovandeginste/workout-tracker/v2/assets" | ||
"github.com/jovandeginste/workout-tracker/v2/pkg/app" | ||
"github.com/jovandeginste/workout-tracker/v2/pkg/version" | ||
apptranslations "github.com/jovandeginste/workout-tracker/v2/translations" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type cli struct { | ||
app *app.App | ||
} | ||
|
||
func newCLI() (*cli, error) { | ||
a := app.NewApp(version.Version{ | ||
BuildTime: buildTime, | ||
Ref: gitRef, | ||
RefName: gitRefName, | ||
RefType: gitRefType, | ||
Sha: gitCommit, | ||
}) | ||
a.Assets = appassets.FS() | ||
a.Translations = apptranslations.FS() | ||
|
||
if err := a.ReadConfiguration(); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := a.ConfigureDatabase(); err != nil { | ||
return nil, err | ||
} | ||
|
||
c := &cli{ | ||
app: a, | ||
} | ||
|
||
return c, nil | ||
} | ||
|
||
func (c *cli) getDatabase() *gorm.DB { | ||
return c.app.DB() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
var ( | ||
gitRef = "0.0.0-dev" | ||
gitRefName = "local" | ||
gitRefType = "local" | ||
gitCommit = "local" | ||
buildTime = "now" | ||
) | ||
|
||
func main() { | ||
c, err := newCLI() | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
if err := c.rootCmd().Execute(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/davecgh/go-spew/spew" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func (c *cli) rootCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "wt-debug", | ||
Short: "Workout Tracker debugger is a CLI tool to debug the workout tracker database", | ||
} | ||
|
||
cmd.AddCommand(c.versionCmd()) | ||
cmd.AddCommand(c.configCmd()) | ||
cmd.AddCommand(c.workoutsCmd()) | ||
|
||
return cmd | ||
} | ||
|
||
func (c *cli) versionCmd() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "version", | ||
Short: "Shows the version of the application", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Println("Version: " + c.app.Version.PrettyVersion()) | ||
fmt.Println("Build time: " + c.app.Version.BuildTime) | ||
}, | ||
} | ||
} | ||
|
||
func (c *cli) configCmd() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "config", | ||
Short: "Show the full configuration of the application (including sensitive information)", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
spew.Dump(c.app.Config) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"strconv" | ||
|
||
"github.com/aquasecurity/table" | ||
"github.com/jovandeginste/workout-tracker/v2/pkg/database" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func (c *cli) workoutsCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "workouts", | ||
Short: "Operate on workouts", | ||
} | ||
|
||
cmd.AddCommand(c.workoutsListCmd()) | ||
cmd.AddCommand(c.workoutsShowCmd()) | ||
|
||
return cmd | ||
} | ||
|
||
func (c *cli) workoutsListCmd() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "list", | ||
Short: "List all workouts", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
t := table.New(os.Stdout) | ||
t.SetHeaders("ID", "Date", "Name") | ||
|
||
workouts, err := database.GetWorkouts(c.getDatabase()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, wo := range workouts { | ||
t.AddRow(strconv.FormatUint(uint64(wo.ID), 10), wo.Date.String(), wo.Name) | ||
} | ||
|
||
t.Render() | ||
|
||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func (c *cli) workoutsShowCmd() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "show", | ||
Short: "Show information about a workout", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
id, err := strconv.Atoi(args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
t := table.New(os.Stdout) | ||
t.SetRowLines(false) | ||
|
||
wo, err := database.GetWorkout(c.getDatabase(), id) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
t.AddRow("ID", strconv.FormatUint(uint64(wo.ID), 10)) | ||
t.AddRow("Date", wo.Date.String()) | ||
t.AddRow("Name", wo.Name) | ||
|
||
t.AddRow("Location", wo.Address()) | ||
t.AddRow("Distance (m)", strconv.FormatFloat(wo.TotalDistance(), 'f', 2, 64)) | ||
t.AddRow("Duration (s)", strconv.FormatFloat(wo.TotalDuration().Seconds(), 'f', 2, 64)) | ||
|
||
t.Render() | ||
|
||
return nil | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,3 +169,7 @@ func (a *App) createAdminUser() error { | |
|
||
return u.Create(a.db) | ||
} | ||
|
||
func (a *App) DB() *gorm.DB { | ||
return a.db | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.