Merge pull request #2095 from marcosnils/feat/init_templates
Allow to specify template when running `dagger project init`
This commit is contained in:
commit
a6396a0a17
@ -33,8 +33,17 @@ var initCmd = &cobra.Command{
|
||||
dir = args[0]
|
||||
}
|
||||
|
||||
name := viper.GetString("name")
|
||||
// TODO @gerhard suggested maybe eventually having a
|
||||
// `dagger project template [list, new]`
|
||||
t := viper.GetString("template")
|
||||
if len(t) > 0 {
|
||||
err := createTemplate(t)
|
||||
if err != nil {
|
||||
lg.Fatal().Err(err).Msg("failed to initialize template")
|
||||
}
|
||||
}
|
||||
|
||||
name := viper.GetString("name")
|
||||
doneCh := common.TrackCommand(ctx, cmd)
|
||||
err := pkg.CueModInit(ctx, dir, name)
|
||||
<-doneCh
|
||||
@ -47,6 +56,11 @@ var initCmd = &cobra.Command{
|
||||
|
||||
func init() {
|
||||
initCmd.Flags().StringP("name", "n", "", "project name")
|
||||
t, err := getTemplateNames()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
initCmd.Flags().StringP("template", "t", "", fmt.Sprintf("Template name %s", t))
|
||||
if err := viper.BindPFlags(initCmd.Flags()); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
50
cmd/dagger/cmd/project/template.go
Normal file
50
cmd/dagger/cmd/project/template.go
Normal file
@ -0,0 +1,50 @@
|
||||
package project
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//go:embed templates/*.cue
|
||||
var templateFS embed.FS
|
||||
|
||||
func createTemplate(name string) error {
|
||||
filename := fmt.Sprintf("%s.cue", name)
|
||||
f, err := templateFS.Open(fmt.Sprintf("templates/%s", filename))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
fout, err := os.Create(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer fout.Close()
|
||||
|
||||
_, err = io.Copy(fout, f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getTemplateNames() ([]string, error) {
|
||||
r := []string{}
|
||||
e, err := templateFS.ReadDir("templates")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, f := range e {
|
||||
r = append(r, strings.TrimSuffix(f.Name(), filepath.Ext(f.Name())))
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
28
cmd/dagger/cmd/project/templates/hello.cue
Normal file
28
cmd/dagger/cmd/project/templates/hello.cue
Normal file
@ -0,0 +1,28 @@
|
||||
// The first time you run the hello
|
||||
// action as `dagger do hello --log-format plain`,
|
||||
// make sure to run `dagger project update` first,
|
||||
// so that all required dependencies are available.
|
||||
|
||||
package hello
|
||||
|
||||
import (
|
||||
"dagger.io/dagger"
|
||||
"universe.dagger.io/bash"
|
||||
"universe.dagger.io/alpine"
|
||||
|
||||
)
|
||||
|
||||
dagger.#Plan & {
|
||||
actions: {
|
||||
_alpine: alpine.#Build & {
|
||||
packages: bash: _
|
||||
}
|
||||
|
||||
// Hello world
|
||||
hello: bash.#Run & {
|
||||
input: _alpine.output
|
||||
script: contents: "echo Hello World"
|
||||
always: true
|
||||
}
|
||||
}
|
||||
}
|
@ -4,7 +4,7 @@
|
||||
"test": "bats --jobs 4 --print-output-on-failure --verbose-run ."
|
||||
},
|
||||
"devDependencies": {
|
||||
"bats": "https://github.com/bats-core/bats-core#master",
|
||||
"bats": "https://github.com/bats-core/bats-core#v1.6.0",
|
||||
"bats-assert": "https://github.com/bats-core/bats-assert",
|
||||
"bats-support": "https://github.com/bats-core/bats-support"
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ setup() {
|
||||
}
|
||||
|
||||
@test "project init and update and info" {
|
||||
cd "$TEMPDIR" || exit
|
||||
cd "$TEMPDIR" || exit 1
|
||||
|
||||
"$DAGGER" project init ./ --name "github.com/foo/bar"
|
||||
test -d ./cue.mod/pkg
|
||||
@ -39,3 +39,27 @@ setup() {
|
||||
assert_failure
|
||||
assert_output --partial "dagger project not found. Run \`dagger project init\`"
|
||||
}
|
||||
|
||||
|
||||
@test "project init with template" {
|
||||
cd "$TEMPDIR" || exit 1
|
||||
|
||||
if test -f ./hello.cue
|
||||
then
|
||||
echo "./hello.cue should not exist"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
run "$DAGGER" project init -t hello
|
||||
|
||||
assert_success
|
||||
|
||||
if test ! -f ./hello.cue
|
||||
then
|
||||
echo "./hello.cue file was not created by the template flag"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cd -
|
||||
diff --unified "$TEMPDIR/hello.cue" "$TESTDIR/../cmd/dagger/cmd/project/templates/hello.cue"
|
||||
}
|
||||
|
Reference in New Issue
Block a user