-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into dependabot/go_modules/go_modules-f1f754ea6b
- Loading branch information
Showing
135 changed files
with
4,047 additions
and
750 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,9 @@ go.mod.bak | |
dist/ | ||
.cache_ggshield | ||
.DS_Store | ||
.project | ||
.classpath | ||
.settings/ | ||
*.exe | ||
.secrets | ||
*.log |
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,110 @@ | ||
package check | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
// bind OCM configuration. | ||
_ "github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi/config" | ||
|
||
"github.com/mandelsoft/goutils/errors" | ||
"github.com/mandelsoft/logging" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/open-component-model/ocm/pkg/contexts/ocm" | ||
) | ||
|
||
const Name = "check" | ||
|
||
var log = logging.DynamicLogger(logging.DefaultContext(), logging.NewRealm("cliplugin/rhabarber")) | ||
|
||
func New() *cobra.Command { | ||
cmd := &command{} | ||
c := &cobra.Command{ | ||
Use: Name + " <options>", | ||
Short: "determine whether we are in rhubarb season", | ||
Long: "The rhubarb season is between march and april.", | ||
RunE: cmd.Run, | ||
} | ||
|
||
c.Flags().StringVarP(&cmd.date, "date", "d", "", "the date to ask for (MM/DD)") | ||
return c | ||
} | ||
|
||
type command struct { | ||
date string | ||
} | ||
|
||
var months = map[string]int{ | ||
"jan": 1, | ||
"feb": 2, | ||
"mär": 3, "mar": 3, | ||
"apr": 4, | ||
"mai": 5, "may": 5, | ||
"jun": 6, | ||
"jul": 7, | ||
"aug": 8, | ||
"sep": 9, | ||
"okt": 10, "oct": 10, | ||
"nov": 11, | ||
"dez": 12, "dec": 12, | ||
} | ||
|
||
func (c *command) Run(cmd *cobra.Command, args []string) error { | ||
season := Season{ | ||
Start: "mar/1", | ||
End: "apr/30", | ||
} | ||
|
||
ctx := ocm.FromContext(cmd.Context()) | ||
ctx.ConfigContext().ApplyTo(0, &season) | ||
|
||
start, err := ParseDate(season.Start) | ||
if err != nil { | ||
return errors.Wrapf(err, "invalid season start") | ||
} | ||
|
||
end, err := ParseDate(season.End) | ||
if err != nil { | ||
return errors.Wrapf(err, "invalid season end") | ||
} | ||
end = end.Add(time.Hour * 24) | ||
|
||
d := time.Now() | ||
if c.date != "" { | ||
d, err = ParseDate(c.date) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
log.Debug("testing rhabarb season", "date", d.String()) | ||
if d.After(start) && d.Before(end) { | ||
fmt.Printf("Yeah, it's rhabarb season - happy rhabarbing!\n") | ||
} else { | ||
fmt.Printf("Sorry, but you have to stay hungry.\n") | ||
} | ||
return nil | ||
} | ||
|
||
func ParseDate(s string) (time.Time, error) { | ||
parts := strings.Split(s, "/") | ||
if len(parts) != 2 { | ||
return time.Time{}, fmt.Errorf("invalid date, expected MM/DD") | ||
} | ||
month, err := strconv.Atoi(parts[0]) | ||
if err != nil { | ||
month = months[strings.ToLower(parts[0])] | ||
if month == 0 { | ||
return time.Time{}, errors.Wrapf(err, "invalid month") | ||
} | ||
} | ||
day, err := strconv.Atoi(parts[1]) | ||
if err != nil { | ||
return time.Time{}, errors.Wrapf(err, "invalid day") | ||
} | ||
|
||
return time.Date(time.Now().Year(), time.Month(month), day, 0, 0, 0, 0, time.Local), nil //nolint:gosmopolitan // yes | ||
} |
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,70 @@ | ||
package check | ||
|
||
import ( | ||
cfgcpi "github.com/open-component-model/ocm/pkg/contexts/config/cpi" | ||
"github.com/open-component-model/ocm/pkg/runtime" | ||
) | ||
|
||
const ( | ||
ConfigType = "rhabarber.config.acme.org" | ||
ConfigTypeV1 = ConfigType + runtime.VersionSeparator + "v1" | ||
) | ||
|
||
var ( | ||
RhabarberType cfgcpi.ConfigType | ||
RhabarberTypeV1 cfgcpi.ConfigType | ||
) | ||
|
||
func init() { | ||
RhabarberType = cfgcpi.NewConfigType[*Config](ConfigType, usage) | ||
cfgcpi.RegisterConfigType(RhabarberType) | ||
RhabarberTypeV1 = cfgcpi.NewConfigType[*Config](ConfigTypeV1, "") | ||
|
||
cfgcpi.RegisterConfigType(RhabarberTypeV1) | ||
} | ||
|
||
type Season struct { | ||
Start string `json:"start"` | ||
End string `json:"end"` | ||
} | ||
|
||
// Config describes a memory based repository interface. | ||
type Config struct { | ||
runtime.ObjectVersionedType `json:",inline"` | ||
Season `json:",inline"` | ||
} | ||
|
||
// NewConfig creates a new memory ConfigSpec. | ||
func NewConfig(start, end string) *Config { | ||
return &Config{ | ||
ObjectVersionedType: runtime.NewVersionedTypedObject(ConfigType), | ||
Season: Season{ | ||
Start: start, | ||
End: end, | ||
}, | ||
} | ||
} | ||
|
||
func (a *Config) GetType() string { | ||
return ConfigType | ||
} | ||
|
||
func (a *Config) ApplyTo(ctx cfgcpi.Context, target interface{}) error { | ||
t, ok := target.(*Season) | ||
if !ok { | ||
return cfgcpi.ErrNoContext(ConfigType) | ||
} | ||
|
||
*t = a.Season | ||
return nil | ||
} | ||
|
||
const usage = ` | ||
The config type <code>` + ConfigType + `</code> can be used to configure the season for rhubarb: | ||
<pre> | ||
type: ` + ConfigType + ` | ||
start: mar/1 | ||
end: apr/30 | ||
</pre> | ||
` |
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,154 @@ | ||
//go:build unix | ||
|
||
package cmds_test | ||
|
||
import ( | ||
"bytes" | ||
|
||
. "github.com/mandelsoft/goutils/testutils" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
. "github.com/open-component-model/ocm/cmds/ocm/testhelper" | ||
. "github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/testutils" | ||
|
||
"github.com/mandelsoft/logging/logrusl" | ||
"github.com/mandelsoft/logging/utils" | ||
|
||
"github.com/open-component-model/ocm/pkg/contexts/ocm/attrs/plugincacheattr" | ||
"github.com/open-component-model/ocm/pkg/version" | ||
) | ||
|
||
const KIND = "rhubarb" | ||
|
||
var _ = Describe("cliplugin", func() { | ||
Context("lib", func() { | ||
var env *TestEnv | ||
var plugins TempPluginDir | ||
|
||
BeforeEach(func() { | ||
env = NewTestEnv(TestData()) | ||
plugins = Must(ConfigureTestPlugins(env, "testdata/plugins")) | ||
|
||
registry := plugincacheattr.Get(env) | ||
// Expect(registration.RegisterExtensions(env)).To(Succeed()) | ||
p := registry.Get("cliplugin") | ||
Expect(p).NotTo(BeNil()) | ||
Expect(p.Error()).To(Equal("")) | ||
}) | ||
|
||
AfterEach(func() { | ||
plugins.Cleanup() | ||
env.Cleanup() | ||
}) | ||
|
||
It("run plugin based ocm command", func() { | ||
var buf bytes.Buffer | ||
|
||
MustBeSuccessful(env.CatchOutput(&buf).Execute("--config", "testdata/config.yaml", "check", KIND, "-d", "jul/10")) | ||
|
||
Expect("\n" + buf.String()).To(Equal(` | ||
Yeah, it's rhabarb season - happy rhabarbing! | ||
`)) | ||
}) | ||
|
||
It("runs plugin based ocm command with log", func() { | ||
var stdout bytes.Buffer | ||
var stdlog bytes.Buffer | ||
|
||
lctx := env.OCMContext().LoggingContext() | ||
lctx.SetBaseLogger(logrusl.WithWriter(utils.NewSyncWriter(&stdlog)).NewLogr()) | ||
MustBeSuccessful(env.CatchOutput(&stdout). | ||
Execute("--config", "testdata/logcfg.yaml", "check", KIND, "-d", "jul/10")) | ||
|
||
Expect("\n" + stdout.String()).To(Equal(` | ||
Yeah, it's rhabarb season - happy rhabarbing! | ||
`)) | ||
// {"date":".*","level":"debug","msg":"testing rhabarb season","realm":"cliplugin/rhabarber","time":".*"} | ||
Expect(stdlog.String()).To(StringMatchTrimmedWithContext(` | ||
[^ ]* debug \[cliplugin/rhabarber\] "testing rhabarb season" date="[^"]*" | ||
`)) | ||
}) | ||
|
||
It("fails for undeclared config", func() { | ||
var buf bytes.Buffer | ||
|
||
Expect(env.CatchOutput(&buf).Execute("--config", "testdata/err.yaml", "check", KIND, "-d", "jul/10")).To( | ||
MatchError(`config type "err.config.acme.org" is unknown`)) | ||
}) | ||
|
||
It("shows command help", func() { | ||
var buf bytes.Buffer | ||
|
||
MustBeSuccessful(env.CatchOutput(&buf).Execute("check", KIND, "--help")) | ||
Expect(buf.String()).To(StringEqualTrimmedWithContext(` | ||
ocm check rhubarb — Determine Whether We Are In Rhubarb Season | ||
Synopsis: | ||
ocm check rhubarb <options> | ||
Flags: | ||
-d, --date string the date to ask for (MM/DD) | ||
-h, --help help for check | ||
Description: | ||
The rhubarb season is between march and april. | ||
`)) | ||
}) | ||
|
||
It("shows command help from main command", func() { | ||
var buf bytes.Buffer | ||
|
||
MustBeSuccessful(env.CatchOutput(&buf).Execute("help", "check", KIND)) | ||
Expect(buf.String()).To(StringEqualTrimmedWithContext(` | ||
ocm check rhubarb — Determine Whether We Are In Rhubarb Season | ||
Synopsis: | ||
ocm check rhubarb <options> | ||
Flags: | ||
-d, --date string the date to ask for (MM/DD) | ||
-h, --help help for check | ||
Description: | ||
The rhubarb season is between march and april. | ||
`)) | ||
}) | ||
|
||
It("describe", func() { | ||
var buf bytes.Buffer | ||
|
||
MustBeSuccessful(env.CatchOutput(&buf).Execute("describe", "plugin", "cliplugin")) | ||
Expect(buf.String()).To(StringEqualTrimmedWithContext(` | ||
Plugin Name: cliplugin | ||
Plugin Version: ` + version.Get().String() + ` | ||
Path: ` + plugins.Path() + `/cliplugin | ||
Status: valid | ||
Source: manually installed | ||
Capabilities: CLI Commands, Config Types | ||
Description: | ||
The plugin offers the check command for object type rhubarb to check the rhubarb season. | ||
CLI Extensions: | ||
- Name: check (determine whether we are in rhubarb season) | ||
Object: rhubarb | ||
Verb: check | ||
Usage: check rhubarb <options> | ||
The rhubarb season is between march and april. | ||
Config Types for CLI Command Extensions: | ||
- Name: rhabarber.config.acme.org | ||
The config type «rhabarber.config.acme.org» can be used to configure the season for rhubarb: | ||
type: rhabarber.config.acme.org | ||
start: mar/1 | ||
end: apr/30 | ||
Versions: | ||
- Version: v1 | ||
*** found 1 plugins | ||
`)) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.