Skip to content

Commit

Permalink
fix: 'pathwar admin challenge-add' improve UX, sane default values, n…
Browse files Browse the repository at this point in the history
…o slug duplicate allowed
  • Loading branch information
moul committed Jul 26, 2020
1 parent 2a8a42f commit ac5d79d
Show file tree
Hide file tree
Showing 8 changed files with 381 additions and 361 deletions.
6 changes: 3 additions & 3 deletions api/pwdb.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ message Challenge {
string author = 102;
string locale = 103;
bool is_draft = 104;
string preview_url = 105;
string preview_url = 105 [(gogoproto.customname) = "PreviewURL"];
string homepage = 106;
string slug = 107;
string slug = 107 [(gogoproto.moretags) = "gorm:\"unique_index\""];
// AuthorProfile
// repeated string tags = ;

Expand Down Expand Up @@ -390,7 +390,7 @@ message Notification {
google.protobuf.Timestamp updated_at = 3 [(gogoproto.stdtime) = true, (gogoproto.nullable) = true];

google.protobuf.Timestamp read_at = 100 [(gogoproto.stdtime) = true, (gogoproto.nullable) = true];
string click_url = 101;
string click_url = 101 [(gogoproto.customname) = "ClickURL"];
string msg = 102;
string args = 103; // marshaled args
// FIXME: get inspiration from push notifications
Expand Down
2 changes: 1 addition & 1 deletion docs/gen.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 25 additions & 16 deletions go/cmd/pathwar/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -691,21 +691,28 @@ func adminRedumpCommand() *ffcli.Command {
}

func adminChallengeAddCommand() *ffcli.Command {
input := pwapi.AdminChallengeAdd_Input{Challenge: &pwdb.Challenge{}}
input.ApplyDefaults()
flags := flag.NewFlagSet("admin challenge add", flag.ExitOnError)
flags.StringVar(&adminChallengeAddInput.Challenge.Name, "name", "", "Challenge name")
flags.StringVar(&adminChallengeAddInput.Challenge.Description, "description", "", "Challenge description")
flags.StringVar(&adminChallengeAddInput.Challenge.Author, "author", "", "Challenge author")
flags.StringVar(&adminChallengeAddInput.Challenge.Locale, "locale", "", "Challenge Locale")
flags.BoolVar(&adminChallengeAddInput.Challenge.IsDraft, "is-draft", true, "Is challenge production ready ?")
flags.StringVar(&adminChallengeAddInput.Challenge.PreviewUrl, "preview-url", "", "Challenge preview URL")
flags.StringVar(&adminChallengeAddInput.Challenge.Homepage, "homepage", "", "Challenge homepage URL")
flags.StringVar(&input.Challenge.Slug, "slug", input.Challenge.Slug, "Unique slug")
flags.StringVar(&input.Challenge.Name, "name", input.Challenge.Name, "Challenge name")
flags.StringVar(&input.Challenge.Description, "description", input.Challenge.Description, "Challenge description")
flags.StringVar(&input.Challenge.Author, "author", input.Challenge.Author, "Challenge author")
flags.StringVar(&input.Challenge.Locale, "locale", input.Challenge.Locale, "Challenge Locale")
flags.BoolVar(&input.Challenge.IsDraft, "is-draft", input.Challenge.IsDraft, "Is challenge production ready ?")
flags.StringVar(&input.Challenge.PreviewURL, "preview-url", input.Challenge.PreviewURL, "Challenge preview URL")
flags.StringVar(&input.Challenge.Homepage, "homepage", input.Challenge.Homepage, "Challenge homepage URL")

return &ffcli.Command{
Name: "challenge-add",
Usage: "pathwar [global flags] admin [admin flags] challenge-add [flags] [args...]",
ShortHelp: "add a challenge",
FlagSet: flags,
Exec: func(args []string) error {
if input.Challenge.Name == "" {
return flag.ErrHelp
}

if err := globalPreRun(); err != nil {
return err
}
Expand All @@ -716,7 +723,7 @@ func adminChallengeAddCommand() *ffcli.Command {
return errcode.TODO.Wrap(err)
}

ret, err := apiClient.AdminAddChallenge(ctx, &adminChallengeAddInput)
ret, err := apiClient.AdminAddChallenge(ctx, &input)
if err != nil {
return errcode.TODO.Wrap(err)
}
Expand All @@ -736,10 +743,11 @@ func adminChallengeAddCommand() *ffcli.Command {
}

func adminChallengeFlavorAddCommand() *ffcli.Command {
input := pwapi.AdminChallengeFlavorAdd_Input{ChallengeFlavor: &pwdb.ChallengeFlavor{}}
flags := flag.NewFlagSet("admin challenge flavor add", flag.ExitOnError)
flags.StringVar(&adminChallengeFlavorAddInput.ChallengeFlavor.Version, "version", "1.0.0", "Challenge flavor version")
flags.StringVar(&adminChallengeFlavorAddInput.ChallengeFlavor.ComposeBundle, "compose-bundle", "", "Challenge flavor compose bundle")
flags.Int64Var(&adminChallengeFlavorAddInput.ChallengeFlavor.ChallengeID, "challenge-id", 0, "Challenge id")
flags.StringVar(&input.ChallengeFlavor.Version, "version", "1.0.0", "Challenge flavor version")
flags.StringVar(&input.ChallengeFlavor.ComposeBundle, "compose-bundle", "", "Challenge flavor compose bundle")
flags.Int64Var(&input.ChallengeFlavor.ChallengeID, "challenge-id", 0, "Challenge id")
return &ffcli.Command{
Name: "challenge-flavor-add",
Usage: "pathwar [global flags] admin [admin flags] challenge-flavor-add [flags] [args...]",
Expand All @@ -756,7 +764,7 @@ func adminChallengeFlavorAddCommand() *ffcli.Command {
return errcode.TODO.Wrap(err)
}

ret, err := apiClient.AdminAddChallengeFlavor(ctx, &adminChallengeFlavorAddInput)
ret, err := apiClient.AdminAddChallengeFlavor(ctx, &input)
if err != nil {
return errcode.TODO.Wrap(err)
}
Expand All @@ -775,9 +783,10 @@ func adminChallengeFlavorAddCommand() *ffcli.Command {
}
}
func adminChallengeInstanceAddCommand() *ffcli.Command {
input := pwapi.AdminChallengeInstanceAdd_Input{ChallengeInstance: &pwdb.ChallengeInstance{}}
flags := flag.NewFlagSet("admin challenge instance add", flag.ExitOnError)
flags.Int64Var(&adminChallengeInstanceAddInput.ChallengeInstance.AgentID, "agent-id", 0, "Id of the agent that will host the instance")
flags.Int64Var(&adminChallengeInstanceAddInput.ChallengeInstance.FlavorID, "flavor-id", 0, "Challenge flavor id")
flags.Int64Var(&input.ChallengeInstance.AgentID, "agent-id", 0, "Id of the agent that will host the instance")
flags.Int64Var(&input.ChallengeInstance.FlavorID, "flavor-id", 0, "Challenge flavor id")
return &ffcli.Command{
Name: "challenge-instance-add",
Usage: "pathwar [global flags] admin [admin flags] challenge-instance-add [flags] [args...]",
Expand All @@ -794,7 +803,7 @@ func adminChallengeInstanceAddCommand() *ffcli.Command {
return errcode.TODO.Wrap(err)
}

ret, err := apiClient.AdminAddChallengeInstance(ctx, &adminChallengeInstanceAddInput)
ret, err := apiClient.AdminAddChallengeInstance(ctx, &input)
if err != nil {
return errcode.TODO.Wrap(err)
}
Expand Down Expand Up @@ -872,7 +881,7 @@ func asciiBool(input bool) string {

func asciiStatus(status string) string {
switch status {
case "Active":
case "Active", "Available":
status += " 🟢"
default:
status += " 🔴"
Expand Down
1 change: 0 additions & 1 deletion go/cmd/pathwar/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ dbConnectLoop:
db, err := gorm.Open("mysql", DBURN)
if err != nil {
dbConnectTries++
fmt.Println(DBMaxOpenTries, dbConnectTries)
if DBMaxOpenTries == 0 || dbConnectTries < DBMaxOpenTries {
logger.Warn("db open", zap.Int("tries", dbConnectTries), zap.Error(err))
time.Sleep(5 * time.Second)
Expand Down
16 changes: 6 additions & 10 deletions go/cmd/pathwar/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"pathwar.land/pathwar/v2/go/pkg/pwagent"
"pathwar.land/pathwar/v2/go/pkg/pwapi"
"pathwar.land/pathwar/v2/go/pkg/pwcompose"
"pathwar.land/pathwar/v2/go/pkg/pwdb"
"pathwar.land/pathwar/v2/go/pkg/pwsso"
"pathwar.land/pathwar/v2/go/pkg/pwversion"
)
Expand All @@ -43,15 +42,12 @@ var (
flagOutput = os.Stderr

// flag vars
adminChallengeAddInput = pwapi.AdminChallengeAdd_Input{Challenge: &pwdb.Challenge{}}
adminChallengeFlavorAddInput = pwapi.AdminChallengeFlavorAdd_Input{ChallengeFlavor: &pwdb.ChallengeFlavor{}}
adminChallengeInstanceAddInput = pwapi.AdminChallengeInstanceAdd_Input{ChallengeInstance: &pwdb.ChallengeInstance{}}
agentOpts = pwagent.NewOpts()
serverOpts = pwapi.NewServerOpts()
ssoOpts = pwsso.NewOpts()
composeCleanOpts = pwcompose.NewCleanOpts()
composePrepareOpts = pwcompose.NewPrepareOpts()
composeUpOpts = pwcompose.NewUpOpts()
agentOpts = pwagent.NewOpts()
serverOpts = pwapi.NewServerOpts()
ssoOpts = pwsso.NewOpts()
composeCleanOpts = pwcompose.NewCleanOpts()
composePrepareOpts = pwcompose.NewPrepareOpts()
composeUpOpts = pwcompose.NewUpOpts()

DBURN string
DBMaxOpenTries int
Expand Down
2 changes: 1 addition & 1 deletion go/gen.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion go/pkg/pwapi/api_admin-challenge-add.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ func (svc *service) AdminChallengeAdd(ctx context.Context, in *AdminChallengeAdd
return nil, errcode.ErrRestrictedArea
}

in.ApplyDefaults()
if in == nil || in.Challenge.Name == "" {
return nil, errcode.ErrMissingInput
}

challenge := pwdb.Challenge{
Slug: in.Challenge.Slug,
Name: in.Challenge.Name,
Description: in.Challenge.Description,
Author: in.Challenge.Author,
Locale: in.Challenge.Locale,
IsDraft: in.Challenge.IsDraft,
PreviewUrl: in.Challenge.PreviewUrl,
PreviewURL: in.Challenge.PreviewURL,
Homepage: in.Challenge.Homepage,
}

Expand All @@ -36,3 +38,15 @@ func (svc *service) AdminChallengeAdd(ctx context.Context, in *AdminChallengeAdd
}
return &out, nil
}

func (in *AdminChallengeAdd_Input) ApplyDefaults() {
if in.Challenge.Locale == "" {
in.Challenge.Locale = "en_US"
}
if in.Challenge.Author == "" {
in.Challenge.Author = "Pathwar Staff"
}
if in.Challenge.Slug != "" && in.Challenge.Name == "" {
in.Challenge.Name = in.Challenge.Slug
}
}
Loading

0 comments on commit ac5d79d

Please sign in to comment.