-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
347 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,60 @@ | ||
package content | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
|
||
"github.com/iximiuz/labctl/internal/labcli" | ||
) | ||
|
||
func NewCommand(cli labcli.CLI) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "content <create|pull|files|sync|rm> <content-name> [flags]", | ||
Use: "content <create|list|pull|files|sync|rm> <content-name> [flags]", | ||
Aliases: []string{"c", "contents"}, | ||
Short: "Authoring and managing content (challenge, tutorial, course, etc.)", | ||
} | ||
|
||
cmd.AddCommand( | ||
newCreateCommand(cli), | ||
newListCommand(cli), | ||
newSyncCommand(cli), | ||
newRemoveCommand(cli), | ||
) | ||
|
||
return cmd | ||
} | ||
|
||
type ContentKind string | ||
|
||
var _ pflag.Value = (*ContentKind)(nil) | ||
|
||
const ( | ||
KindChallenge ContentKind = "challenge" | ||
KindTutorial ContentKind = "tutorial" | ||
KindCourse ContentKind = "course" | ||
) | ||
|
||
func (k *ContentKind) Set(v string) error { | ||
switch string(v) { | ||
case string(KindChallenge): | ||
*k = KindChallenge | ||
case string(KindTutorial): | ||
*k = KindTutorial | ||
case string(KindCourse): | ||
*k = KindCourse | ||
default: | ||
return fmt.Errorf("unknown content kind: %s", v) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (k *ContentKind) String() string { | ||
return string(*k) | ||
} | ||
|
||
func (k *ContentKind) Type() string { | ||
return "content-kind" | ||
} |
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,73 @@ | ||
package content | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v2" | ||
|
||
"github.com/iximiuz/labctl/internal/api" | ||
"github.com/iximiuz/labctl/internal/labcli" | ||
) | ||
|
||
type listOptions struct { | ||
kind ContentKind | ||
} | ||
|
||
func newListCommand(cli labcli.CLI) *cobra.Command { | ||
var opts listOptions | ||
|
||
cmd := &cobra.Command{ | ||
Use: "list [--kind challenge|tutorial|course]", | ||
Short: "List authored content, possibly filtered by kind.", | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return labcli.WrapStatusError(runListContent(cmd.Context(), cli, &opts)) | ||
}, | ||
} | ||
|
||
flags := cmd.Flags() | ||
|
||
flags.Var( | ||
&opts.kind, | ||
"kind", | ||
`Content kind to filter by - one of challenge, tutorial, course (an empty string means all)`, | ||
) | ||
|
||
return cmd | ||
} | ||
|
||
type AuthoredContent struct { | ||
Challenges []api.Challenge `json:"challenges" yaml:"challenges"` | ||
Tutorials []api.Tutorial `json:"tutorials" yaml:"tutorials"` | ||
// Courses []api.Course `json:"courses" yaml:"courses"` | ||
} | ||
|
||
func runListContent(ctx context.Context, cli labcli.CLI, opts *listOptions) error { | ||
var content AuthoredContent | ||
|
||
if opts.kind == "" || opts.kind == KindChallenge { | ||
challenges, err := cli.Client().ListAuthoredChallenges(ctx) | ||
if err != nil { | ||
return fmt.Errorf("cannot list authored challenges: %w", err) | ||
} | ||
|
||
content.Challenges = challenges | ||
} | ||
|
||
if opts.kind == "" || opts.kind == KindTutorial { | ||
tutorials, err := cli.Client().ListAuthoredTutorials(ctx) | ||
if err != nil { | ||
return fmt.Errorf("cannot list authored tutorials: %w", err) | ||
} | ||
|
||
content.Tutorials = tutorials | ||
} | ||
|
||
if err := yaml.NewEncoder(cli.OutputStream()).Encode(content); err != nil { | ||
return err | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package content | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/iximiuz/labctl/internal/labcli" | ||
) | ||
|
||
type removeOptions struct { | ||
kind ContentKind | ||
name string | ||
force bool | ||
} | ||
|
||
func newRemoveCommand(cli labcli.CLI) *cobra.Command { | ||
var opts removeOptions | ||
|
||
cmd := &cobra.Command{ | ||
Use: "remove [flags] <challenge|tutorial|course> <name>", | ||
Short: "Remove a piece of content you authored.", | ||
Args: cobra.ExactArgs(2), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if err := opts.kind.Set(args[0]); err != nil { | ||
return labcli.WrapStatusError(err) | ||
} | ||
opts.name = args[1] | ||
|
||
return labcli.WrapStatusError(runRemoveContent(cmd.Context(), cli, &opts)) | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func runRemoveContent(ctx context.Context, cli labcli.CLI, opts *removeOptions) error { | ||
cli.PrintAux("Removing %s %s...\n", opts.kind, opts.name) | ||
|
||
if !opts.force { | ||
if !cli.Confirm( | ||
"This action is irreversible. Are you sure?", | ||
"Yes", "No", | ||
) { | ||
return labcli.NewStatusError(0, "Glad you changed your mind!") | ||
} | ||
} | ||
|
||
switch opts.kind { | ||
case KindChallenge: | ||
return cli.Client().DeleteChallenge(ctx, opts.name) | ||
|
||
case KindTutorial: | ||
return cli.Client().DeleteTutorial(ctx, opts.name) | ||
|
||
case KindCourse: | ||
return fmt.Errorf("removing courses is not supported yet") | ||
|
||
default: | ||
return fmt.Errorf("unknown content kind %q", opts.kind) | ||
} | ||
} |
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,67 @@ | ||
package content | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/iximiuz/labctl/internal/api" | ||
"github.com/iximiuz/labctl/internal/labcli" | ||
) | ||
|
||
type syncOptions struct { | ||
dir string | ||
} | ||
|
||
func newSyncCommand(cli labcli.CLI) *cobra.Command { | ||
var opts syncOptions | ||
|
||
cmd := &cobra.Command{ | ||
Use: "sync [flags] [content-dir]", | ||
Short: "Sync a local directory to remote content storage - the main content authoring routine.", | ||
Args: cobra.MaximumNArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(args) == 1 { | ||
opts.dir = args[0] | ||
} else { | ||
if cwd, err := os.Getwd(); err != nil { | ||
return labcli.WrapStatusError(fmt.Errorf("couldn't get the current working directory: %w", err)) | ||
} else { | ||
opts.dir = cwd | ||
} | ||
} | ||
|
||
return labcli.WrapStatusError(runSyncContent(cmd.Context(), cli, &opts)) | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func runSyncContent(ctx context.Context, cli labcli.CLI, opts *syncOptions) error { | ||
cli.PrintAux("Starting content sync in %s...\n", opts.dir) | ||
|
||
for ctx.Err() == nil { | ||
data, err := os.ReadFile(filepath.Join(opts.dir, "index.md")) | ||
if err != nil { | ||
return fmt.Errorf("couldn't read index.md: %w", err) | ||
} | ||
|
||
if err := cli.Client().PutMarkdown(ctx, api.PutMarkdownRequest{ | ||
Kind: "challenge", | ||
Name: "foobar-qux", | ||
Content: string(data), | ||
}); err != nil { | ||
return fmt.Errorf("couldn't update content: %w", err) | ||
} | ||
|
||
cli.PrintAux("Synced content...\n") | ||
time.Sleep(5 * time.Second) | ||
} | ||
|
||
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
Oops, something went wrong.