Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iximiuz committed Mar 18, 2024
1 parent 5ebdfde commit b03e689
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 33 deletions.
82 changes: 49 additions & 33 deletions cmd/content/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"

"github.com/charmbracelet/huh"
Expand All @@ -22,7 +20,7 @@ type createOptions struct {

dir string

noSample bool
// noSample bool
}

func newCreateCommand(cli labcli.CLI) *cobra.Command {
Expand All @@ -38,31 +36,18 @@ func newCreateCommand(cli labcli.CLI) *cobra.Command {
}
opts.name = args[1]

if opts.dir == "" {
if cwd, err := os.Getwd(); err != nil {
return labcli.WrapStatusError(fmt.Errorf("couldn't get the current working directory: %w", err))
} else {
opts.dir = filepath.Join(cwd, opts.name)
}
}
if absDir, err := filepath.Abs(opts.dir); err != nil {
return labcli.WrapStatusError(fmt.Errorf("couldn't get the absolute path of %s: %w", opts.dir, err))
} else {
opts.dir = absDir
}

return labcli.WrapStatusError(runCreateContent(cmd.Context(), cli, &opts))
},
}

flags := cmd.Flags()

flags.BoolVar(
&opts.noSample,
"no-sample",
false,
`Don't create a sample piece of content`,
)
// flags.BoolVar(
// &opts.noSample,
// "no-sample",
// false,
// `Don't create a sample piece of content`,
// )
flags.StringVar(
&opts.dir,
"dir",
Expand All @@ -87,15 +72,33 @@ func runCreateContent(ctx context.Context, cli labcli.CLI, opts *createOptions)
}
}

cli.PrintAux("Creating a new %s in %s...\n", opts.kind, opts.dir)

if _, err := os.Stat(opts.dir); err == nil {
return fmt.Errorf("directory %s already exists - aborting to avoid overwriting existing files", opts.dir)
}

if err := os.MkdirAll(opts.dir, 0755); err != nil {
return fmt.Errorf("couldn't create directory %s: %w", opts.dir, err)
}
cli.PrintAux("Creating a new %s...\n", opts.kind)

// if _, err := os.Stat(opts.dir); err == nil {
// return fmt.Errorf("directory %s already exists - aborting to avoid overwriting existing files", opts.dir)
// }

// 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
// }
// if opts.dir == "" {
// 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
// }
// }
// if absDir, err := filepath.Abs(opts.dir); err != nil {
// return labcli.WrapStatusError(fmt.Errorf("couldn't get the absolute path of %s: %w", opts.dir, err))
// } else {
// opts.dir = absDir
// }

// if err := os.MkdirAll(opts.dir, 0755); err != nil {
// return fmt.Errorf("couldn't create directory %s: %w", opts.dir, err)
// }

switch opts.kind {
case KindChallenge:
Expand Down Expand Up @@ -167,15 +170,28 @@ This is a sample challenge. You can edit this file in ... .`,
return fmt.Errorf("couldn't create a sample markdown file: %w", err)
}

return labcli.NewStatusError(0, "Happy authoring!")
return labcli.NewStatusError(0, "Happy authoring..")
}

func createTutorial(ctx context.Context, cli labcli.CLI, opts *createOptions) error {
return nil
}

func createCourse(ctx context.Context, cli labcli.CLI, opts *createOptions) error {
return nil
ch, err := cli.Client().CreateCourse(ctx, api.CreateCourseRequest{
Name: opts.name,
Variant: api.CourseVariantModular,
})
if err != nil {
return fmt.Errorf("couldn't create course: %w", err)
}

cli.PrintAux("Created a new course %s\n", ch.PageURL)
if err := open.Run(ch.PageURL); err != nil {
cli.PrintAux("Couldn't open the browser. Copy the above URL into a browser manually.\n")
}

return labcli.NewStatusError(0, "Happy authoring..")
}

func hasAuthorProfile(ctx context.Context, cli labcli.CLI) (bool, error) {
Expand Down
60 changes: 60 additions & 0 deletions internal/api/course.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package api

import (
"context"
)

type Course struct {
CreatedAt string `json:"createdAt" yaml:"createdAt"`
UpdatedAt string `json:"updatedAt" yaml:"updatedAt"`

Name string `json:"name" yaml:"name"`

PageURL string `json:"pageUrl" yaml:"pageUrl"`
}

type CourseVariant string

const (
CourseVariantSimple CourseVariant = "simple"
CourseVariantModular CourseVariant = "modular"
)

type CreateCourseRequest struct {
Name string `json:"name"`
Variant CourseVariant `json:"variant"`
}

func (c *Client) CreateCourse(ctx context.Context, req CreateCourseRequest) (*Course, error) {
body, err := toJSONBody(req)
if err != nil {
return nil, err
}

var course Course
return &course, c.PostInto(ctx, "/courses", nil, nil, body, &course)
}

func (c *Client) GetCourse(ctx context.Context, name string) (*Course, error) {
var course Course
return &course, c.GetInto(ctx, "/courses/"+name, nil, nil, &course)
}

func (c *Client) ListCourses(ctx context.Context) ([]Course, error) {
var courses []Course
return courses, c.GetInto(ctx, "/courses", nil, nil, &courses)
}

func (c *Client) ListAuthoredCourses(ctx context.Context) ([]Course, error) {
var courses []Course
return courses, c.GetInto(ctx, "/courses/authored", nil, nil, &courses)
}

func (c *Client) DeleteCourse(ctx context.Context, name string) error {
resp, err := c.Delete(ctx, "/courses/"+name, nil, nil)
if err != nil {
return err
}
resp.Body.Close()
return nil
}

0 comments on commit b03e689

Please sign in to comment.