-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add whitegold pattern * Fix typo
- Loading branch information
Showing
10 changed files
with
1,040 additions
and
0 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
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,53 @@ | ||
package whitegold | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"github.com/giantswarm/microerror" | ||
"github.com/giantswarm/micrologger" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
name = "whitegold" | ||
description = "Draws MarcelMues 'whitegold'." | ||
) | ||
|
||
type Config struct { | ||
Logger micrologger.Logger | ||
Stderr io.Writer | ||
Stdout io.Writer | ||
} | ||
|
||
func New(config Config) (*cobra.Command, error) { | ||
if config.Logger == nil { | ||
return nil, microerror.Maskf(invalidConfigError, "%T.Logger must not be empty", config) | ||
} | ||
if config.Stderr == nil { | ||
config.Stderr = os.Stderr | ||
} | ||
if config.Stdout == nil { | ||
config.Stdout = os.Stdout | ||
} | ||
|
||
f := &flag{} | ||
|
||
r := &runner{ | ||
flag: f, | ||
logger: config.Logger, | ||
stderr: config.Stderr, | ||
stdout: config.Stdout, | ||
} | ||
|
||
c := &cobra.Command{ | ||
Use: name, | ||
Short: description, | ||
Long: description, | ||
RunE: r.Run, | ||
} | ||
|
||
f.Init(c) | ||
|
||
return c, 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,88 @@ | ||
package whitegold | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/giantswarm/micrologger" | ||
) | ||
|
||
func Test_command(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
c1 string | ||
c2 string | ||
filename string | ||
height int | ||
width int | ||
}{ | ||
{ | ||
name: "case 0: default pattern", | ||
filename: "whitegold", | ||
}, | ||
{ | ||
name: "case 1: au color pattern", | ||
filename: "whitegold-au", | ||
c1: "#dff9fb", | ||
c2: "#6ab04c", | ||
}, | ||
{ | ||
name: "case 2: banner resize", | ||
filename: "whitegold-wide", | ||
width: 2000, | ||
height: 400, | ||
}, | ||
} | ||
|
||
for i, tc := range testCases { | ||
t.Run(strconv.Itoa(i), func(t *testing.T) { | ||
t.Log(tc.name) | ||
|
||
var err error | ||
|
||
var logger micrologger.Logger | ||
{ | ||
c := micrologger.Config{} | ||
|
||
logger, err = micrologger.New(c) | ||
if err != nil { | ||
t.Fatal("expected", nil, "got", err) | ||
} | ||
} | ||
|
||
c := Config{ | ||
Logger: logger, | ||
} | ||
|
||
testCommand, err := New(c) | ||
if err != nil { | ||
t.Fatal("expected", nil, "got", err) | ||
} | ||
|
||
args := []string{} | ||
if tc.filename != "" { | ||
args = append(args, []string{"--filename", fmt.Sprintf("../../../samples/%s.svg", tc.filename)}...) | ||
} | ||
if tc.c1 != "" { | ||
args = append(args, []string{"--color1", tc.c1}...) | ||
} | ||
if tc.c2 != "" { | ||
args = append(args, []string{"--color2", tc.c2}...) | ||
} | ||
if tc.height != 0 { | ||
args = append(args, []string{"--height", strconv.Itoa(tc.height)}...) | ||
} | ||
if tc.width != 0 { | ||
args = append(args, []string{"--width", strconv.Itoa(tc.width)}...) | ||
} | ||
|
||
testCommand.SetArgs(args) | ||
|
||
err = testCommand.Execute() | ||
if err != nil { | ||
t.Fatal("expected", nil, "got", err) | ||
} | ||
}) | ||
} | ||
} |
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,21 @@ | ||
package whitegold | ||
|
||
import "github.com/giantswarm/microerror" | ||
|
||
var invalidConfigError = µerror.Error{ | ||
Kind: "invalidConfigError", | ||
} | ||
|
||
// IsInvalidConfig asserts invalidConfigError. | ||
func IsInvalidConfig(err error) bool { | ||
return microerror.Cause(err) == invalidConfigError | ||
} | ||
|
||
var invalidFlagError = µerror.Error{ | ||
Kind: "invalidFlagError", | ||
} | ||
|
||
// IsInvalidFlag asserts invalidFlagError. | ||
func IsInvalidFlag(err error) bool { | ||
return microerror.Cause(err) == invalidFlagError | ||
} |
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,65 @@ | ||
package whitegold | ||
|
||
import ( | ||
"github.com/giantswarm/microerror" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/marcelmue/konstrukt/pkg/validate" | ||
) | ||
|
||
const ( | ||
flagFilename = "filename" | ||
|
||
flagHeight = "height" | ||
flagWidth = "width" | ||
|
||
flagColor1 = "color1" | ||
flagColor2 = "color2" | ||
|
||
flagRandomize = "randomize" | ||
) | ||
|
||
type flag struct { | ||
Filename string | ||
|
||
Height int | ||
Width int | ||
|
||
Color1 string | ||
Color2 string | ||
|
||
Randomize bool | ||
} | ||
|
||
func (f *flag) Init(cmd *cobra.Command) { | ||
cmd.Flags().StringVar(&f.Filename, flagFilename, "whitegold.svg", `Name of the output file.`) | ||
|
||
cmd.Flags().IntVar(&f.Height, flagHeight, 500, `Height of the output file in pixels.`) | ||
cmd.Flags().IntVar(&f.Width, flagWidth, 500, `Width of the output file in pixels.`) | ||
|
||
cmd.Flags().StringVar(&f.Color1, flagColor1, "#130f40", `First color.`) | ||
cmd.Flags().StringVar(&f.Color2, flagColor2, "#ffd32a", `Second color.`) | ||
|
||
cmd.Flags().BoolVar(&f.Randomize, flagRandomize, false, "Randomize all colors in the pattern, ignore other color flags.") | ||
} | ||
|
||
func (f *flag) Validate() error { | ||
if !validate.Color(f.Color1) { | ||
validate.PrintFailure(flagColor1) | ||
return microerror.Mask(invalidFlagError) | ||
} | ||
if !validate.Color(f.Color2) { | ||
validate.PrintFailure(flagColor2) | ||
return microerror.Mask(invalidFlagError) | ||
} | ||
if !validate.Size(f.Height) { | ||
validate.PrintFailure(flagHeight) | ||
return microerror.Mask(invalidFlagError) | ||
} | ||
if !validate.Size(f.Width) { | ||
validate.PrintFailure(flagWidth) | ||
return microerror.Mask(invalidFlagError) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.