Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add init command to initialize a hello world sample API with GoFr #3

Merged
merged 9 commits into from
Aug 26, 2024
78 changes: 78 additions & 0 deletions bootstrap/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package bootstrap

import (
"fmt"
"os"
"text/template"

"gofr.dev/pkg/gofr"
)

const (
modContent = `module {{ .Module }}

go 1.22.4

require gofr.dev v{{ .GofrVersion }}
`
mainContent = `package main

import (
"gofr.dev/pkg/gofr"
)

func main() {
app := gofr.New()

app.GET("/hello", func(ctx *gofr.Context) (interface{}, error) {
return "Hello World!", nil
})

app.Run()
}
`
fileMode = 0644
)

type modInfo struct {
Module string
GofrVersion string
}

func Create(ctx *gofr.Context) (interface{}, error) {
name := ctx.Param("name")
gofrVersion := ctx.Param("gofr")

if gofrVersion == "" {
gofrVersion = "1.17.0"
}

modFile, err := os.OpenFile("go.mod", os.O_CREATE|os.O_WRONLY, fileMode)
if err != nil {
return nil, err
}

t, err := template.New("go-mod").Parse(modContent)
if err != nil {
return nil, err
}

err = t.Execute(modFile, modInfo{Module: name, GofrVersion: gofrVersion})
if err != nil {
return nil, err
}

fmt.Println("Note: Please do go mod tidy to sync the dependencies of your project")

mainFile, err := os.OpenFile("main.go", os.O_CREATE|os.O_WRONLY, fileMode)
if err != nil {
return nil, err
}

_, err = mainFile.WriteString(mainContent)
if err != nil {
return nil, err
}

return "Successfully initialized project " + name, nil
}
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0N
github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package main
import (
"gofr.dev/pkg/gofr"

"gofr.dev/gofr-cli/bootstrap"
"gofr.dev/gofr-cli/migration"
)

func main() {
cli := gofr.NewCMD()

cli.SubCommand("init", bootstrap.Create)

cli.SubCommand("version",
func(*gofr.Context) (interface{}, error) {
return CLIVersion, nil
Expand Down
20 changes: 14 additions & 6 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"gofr.dev/pkg/gofr/testutil"
)

func Test_Main_Version(t *testing.T) {
old := os.Args
os.Args = []string{"gofr-cli", "version"}

t.Cleanup(func() {
os.Args = old
})
setArgs(t, "version")

// test util replaces the stdout with a buffer and returns us
// any output that is printed on the stdout
Expand All @@ -24,3 +20,15 @@ func Test_Main_Version(t *testing.T) {

assert.Contains(t, out, "dev")
}

func setArgs(t *testing.T, args ...string) {
t.Helper()

oldArgs := os.Args

os.Args = append([]string{"gofr-cli"}, args...)

t.Cleanup(func() {
os.Args = oldArgs
})
}
Loading