Skip to content

Commit

Permalink
simplifies commands
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhuber committed Aug 4, 2023
1 parent 4cdeb73 commit 6ad5db9
Show file tree
Hide file tree
Showing 18 changed files with 286 additions and 101 deletions.
1 change: 1 addition & 0 deletions cmd/caster/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func main() {
Commands: []*cli.Command{
commands.Apply,
commands.Interpolate,
commands.Initialize,
},
}
err := app.Run(os.Args)
Expand Down
21 changes: 21 additions & 0 deletions docker/Dockerfile.int.linux
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM golang:latest as build

ADD . ./src

WORKDIR /go/src

RUN mkdir -p ./dist && go build -o ./dist/caster ./cmd/caster/main.go

FROM ubuntu:22.04
RUN apt-get update && apt-get install -y ca-certificates openssl

ARG cert_location=/usr/local/share/ca-certificates

# Get certificate from "github.com"
RUN openssl s_client -showcerts -connect github.com:443 </dev/null 2>/dev/null|openssl x509 -outform PEM > ${cert_location}/github.crt
# Get certificate from "proxy.golang.org"
RUN openssl s_client -showcerts -connect proxy.golang.org:443 </dev/null 2>/dev/null|openssl x509 -outform PEM > ${cert_location}/proxy.golang.crt
# Update certificates
RUN update-ca-certificates

COPY --from=build /go/src/dist/caster /tmp/caster
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/onsi/ginkgo/v2 v2.9.2
github.com/onsi/gomega v1.27.4
github.com/patrickhuber/go-di v0.5.2
github.com/patrickhuber/go-xplat v0.2.12
github.com/patrickhuber/go-xplat v0.2.15
github.com/urfave/cli/v2 v2.23.4
gopkg.in/yaml.v3 v3.0.1
)
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ github.com/patrickhuber/go-di v0.5.2 h1:J2n0tT2D7Lfdwz9qrxbxlHU15QiQ9e6cYmcAvcly
github.com/patrickhuber/go-di v0.5.2/go.mod h1:2Hpv3zqwKfVzJnm1P6f17J1tzWDAFaALpfWCHwXjcmw=
github.com/patrickhuber/go-xplat v0.2.12 h1:sFboVWzBBtOrNzYJerq7dCKmWlc9lIGvbOWKUav6yGc=
github.com/patrickhuber/go-xplat v0.2.12/go.mod h1:ZveaAmQiWcZO0nWACUrbylAF+BbGwEXdVqM3ZoEOxA4=
github.com/patrickhuber/go-xplat v0.2.13 h1:RUzldyyNjnYgQkHHLdtVe/GF6vZhXFwNHMdYEJeodz4=
github.com/patrickhuber/go-xplat v0.2.13/go.mod h1:ZveaAmQiWcZO0nWACUrbylAF+BbGwEXdVqM3ZoEOxA4=
github.com/patrickhuber/go-xplat v0.2.14 h1:RR3C9YbJ02XeS6CXrqRiSUor+W2UxOENnHr+FmZFi64=
github.com/patrickhuber/go-xplat v0.2.14/go.mod h1:ZveaAmQiWcZO0nWACUrbylAF+BbGwEXdVqM3ZoEOxA4=
github.com/patrickhuber/go-xplat v0.2.15 h1:qEqPqjmLp0yqHFCWqE+s41ni55JKs0DhHbK3Mt6xZFo=
github.com/patrickhuber/go-xplat v0.2.15/go.mod h1:ZveaAmQiWcZO0nWACUrbylAF+BbGwEXdVqM3ZoEOxA4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
Expand Down
4 changes: 2 additions & 2 deletions internal/cast/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ type Service interface {

type service struct {
fs afs.FS
path filepath.Processor
path *filepath.Processor
inter interpolate.Service
}

// NewService creates a new instance of the cast service
func NewService(fs afs.FS, inter interpolate.Service, path filepath.Processor) Service {
func NewService(fs afs.FS, inter interpolate.Service, path *filepath.Processor) Service {
return &service{
fs: fs,
inter: inter,
Expand Down
7 changes: 4 additions & 3 deletions internal/cast/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/patrickhuber/go-xplat/env"
"github.com/patrickhuber/go-xplat/filepath"
afs "github.com/patrickhuber/go-xplat/fs"
"github.com/patrickhuber/go-xplat/platform"
"github.com/patrickhuber/go-xplat/os"
)

type ServiceTest interface {
Expand All @@ -28,13 +28,14 @@ type ServiceTest interface {

type serviceTest struct {
fs afs.FS
path filepath.Processor
path *filepath.Processor
env env.Environment
inter interpolate.Service
}

func NewServiceTest() ServiceTest {
path := filepath.NewProcessorWithPlatform(platform.Linux)
o := os.NewLinuxMock()
path := filepath.NewProcessorWithOS(o)
fs := afs.NewMemory(afs.WithProcessor(path))
e := env.NewMemory()

Expand Down
13 changes: 0 additions & 13 deletions internal/commands/commands_suite_test.go

This file was deleted.

54 changes: 54 additions & 0 deletions internal/commands/initialize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package commands

import (
"github.com/patrickhuber/caster/internal/global"
"github.com/patrickhuber/caster/internal/initialize"
"github.com/patrickhuber/go-di"
"github.com/urfave/cli/v2"
)

const (
InitializeTemplateFlag = "template"
)

var Initialize = &cli.Command{
Name: "initialize",
Aliases: []string{"init"},
Description: "initializes the speified directory or file with the default template",
Usage: "initializes the speified directory or file with the default template",
UsageText: "caster init [DIRECTORY|FIILE]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: InitializeTemplateFlag,
Aliases: []string{"t"},
Value: ".",
},
},
}

type InitializeCommand struct {
Options InitializeOptions
Service initialize.Service `inject:""`
}

type InitializeOptions struct {
Template string
}

func InitAction(ctx *cli.Context) error {
cmd := &InitializeCommand{}
resolver := ctx.App.Metadata[global.DependencyInjectionContainer].(di.Resolver)
err := di.Inject(resolver, cmd)
if err != nil {
return err
}
return cmd.Execute()
}

func (cmd *InitializeCommand) Execute() error {
request := &initialize.Request{
Template: cmd.Options.Template,
}
_, err := cmd.Service.Initialize(request)
return err
}
7 changes: 7 additions & 0 deletions internal/commands/initialize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package commands

import "testing"

func TestInitialize(t *testing.T) {

}
5 changes: 3 additions & 2 deletions internal/commands/interpolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ import (
)

const (
InterpolateTemplateFlag = "interpolate"
InterpolateTemplateFlag = "template"
InterpolateNameFlag = "name"
InterpolateVarFlag = "var"
InterpolateVarFileFlag = "var-file"
)

var Interpolate = &cli.Command{
Name: "interpolate",
Aliases: []string{"inter"},
Description: "interpolates the specified template and outputs the result",
Usage: "interpolates the specified template and outputs the result",
UsageText: "caster apply [-t|--template <TEMPLATEDIR|TEMPLATEFILE>] [-n|--name <TEMPLATENAME>]",
UsageText: "caster interpolate [-t|--template <TEMPLATEDIR|TEMPLATEFILE>] [-n|--name <TEMPLATENAME>]",
Action: InterpolateAction,
Flags: []cli.Flag{
&cli.StringFlag{
Expand Down
13 changes: 10 additions & 3 deletions internal/commands/interpolate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/patrickhuber/go-xplat/console"
"github.com/patrickhuber/go-xplat/env"
"github.com/patrickhuber/go-xplat/fs"
"github.com/patrickhuber/go-xplat/os"
"github.com/stretchr/testify/require"
"github.com/urfave/cli/v2"
)
Expand Down Expand Up @@ -147,7 +148,7 @@ func TestInterpolate(t *testing.T) {

t.Run("default", func(t *testing.T) {
cx := SetupInterpolateTestContext(t)
cx.f.WriteFile("/.caster.yml", []byte("files:\n- name: test.txt\n content: {{.key}}"), 0600)
cx.f.WriteFile("/working/.caster.yml", []byte("files:\n- name: test.txt\n content: {{.key}}"), 0600)
cx.f.WriteFile("/data/1.yml", []byte("key: second"), 0600)

args := []string{"caster", "interpolate", "--var", "key=first", "--var-file", "/data/1.yml"}
Expand All @@ -171,15 +172,21 @@ func SetupInterpolateTestContext(t *testing.T) *InterpolateTestContext {
s := setup.NewTest()
container := s.Container()

o, err := di.Resolve[os.OS](container)
require.NoError(t, err)

wd, err := o.WorkingDirectory()
require.NoError(t, err)

con, err := di.Resolve[console.Console](container)
require.NoError(t, err)

f, err := di.Resolve[fs.FS](container)
require.NoError(t, err)

paths := []string{"/", "/template", "/data"}
paths := []string{"/", "/template", "/data", wd, o.Home()}
for _, p := range paths {
err = f.Mkdir(p, 0666)
err = f.MkdirAll(p, 0666)
require.NoError(t, err)
}

Expand Down
53 changes: 53 additions & 0 deletions internal/initialize/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package initialize

import (
"github.com/patrickhuber/go-xplat/filepath"
"github.com/patrickhuber/go-xplat/fs"
)

type Request struct {
Template string `yaml:"omitempty"`
}

type Response struct{}

type Service interface {
Initialize(req *Request) (*Response, error)
}

func NewService(fs fs.FS, path *filepath.Processor) Service {
return &service{
fs: fs,
path: path,
}
}

type service struct {
fs fs.FS
path *filepath.Processor
}

func (s *service) Initialize(req *Request) (*Response, error) {

// look for relative paths
template, err := s.path.Abs(req.Template)
if err != nil {
return nil, err
}

// is this a file or directory?
stat, err := s.fs.Stat(template)
if err != nil {
return nil, err
}
if stat.IsDir() {
template = s.path.Join(template, ".caster.yml")
}

content := `files:
- name: hello.txt
content: "hello world"`

err = s.fs.WriteFile(template, []byte(content), 0666)
return &Response{}, err
}
37 changes: 37 additions & 0 deletions internal/initialize/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package initialize_test

import (
"testing"

"github.com/patrickhuber/caster/internal/initialize"
"github.com/patrickhuber/go-xplat/filepath"
"github.com/patrickhuber/go-xplat/fs"
"github.com/patrickhuber/go-xplat/os"
"github.com/stretchr/testify/require"
)

func TestService(t *testing.T) {
o := os.NewLinuxMock()
path := filepath.NewProcessorWithOS(o)
fs := fs.NewMemory(fs.WithProcessor(path))

svc := initialize.NewService(fs, path)

wd, err := o.WorkingDirectory()
require.NoError(t, err)
err = fs.MkdirAll(wd, 0666)
require.NoError(t, err)

res, err := svc.Initialize(&initialize.Request{})
require.NoError(t, err)
require.NotNil(t, res)

filePath := path.Join(wd, ".caster.yml")
ok, err := fs.Exists(filePath)
require.NoError(t, err)
require.True(t, ok)

content, err := fs.ReadFile(filePath)
require.NoError(t, err)
require.NotNil(t, content)
}
13 changes: 0 additions & 13 deletions internal/interpolate/interpolate_suite_test.go

This file was deleted.

19 changes: 13 additions & 6 deletions internal/interpolate/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Service interface {
}

// NewService creates a new instance of the cast service
func NewService(fs afs.FS, env env.Environment, path filepath.Processor) Service {
func NewService(fs afs.FS, env env.Environment, path *filepath.Processor) Service {
return &service{
fs: fs,
env: env,
Expand All @@ -33,7 +33,7 @@ func NewService(fs afs.FS, env env.Environment, path filepath.Processor) Service

type service struct {
fs afs.FS
path filepath.Processor
path *filepath.Processor
env env.Environment
}

Expand Down Expand Up @@ -142,6 +142,13 @@ func (s *service) getCasterFile(req *Request) (string, error) {
return "", fmt.Errorf("template file is missing")
}

// look for relative paths
abs, err := s.path.Abs(template)
if err != nil {
return "", err
}
template = abs

// is this a file or directory?
info, err := s.fs.Stat(template)
if errors.Is(err, fs.ErrNotExist) {
Expand All @@ -159,14 +166,14 @@ func (s *service) getCasterFile(req *Request) (string, error) {

// read the caster file in the directory
// if it doesn't exist, return an error saying not found
files, err := s.readDirRegex(req.Template, "[.]caster[.](yml|json)")
files, err := s.readDirRegex(template, "[.]caster[.](yml|json)")
if err != nil {
return "", err
return "", fmt.Errorf("%w : template folder '%s' missing .caster.(yml|json) file", err, template)
}
if len(files) == 0 {
return "", fmt.Errorf("template folder '%s' missing .caster.(yml|json) file", req.Template)
return "", fmt.Errorf("template folder '%s' missing .caster.(yml|json) file", template)
}
return s.path.Join(req.Template, files[0].Name()), nil
return s.path.Join(template, files[0].Name()), nil
}

func (s *service) getCasterFileContent(path string) (string, error) {
Expand Down
Loading

0 comments on commit 6ad5db9

Please sign in to comment.