diff --git a/internal/command/box/common.go b/internal/command/box/common.go index 01c94355..36f55a97 100644 --- a/internal/command/box/common.go +++ b/internal/command/box/common.go @@ -144,13 +144,7 @@ func newCreateOptions(info *template.TemplateInfo[boxModel.BoxV1], labels common return nil, err } - var allLabels commonModel.Labels - switch info.SourceType { - case template.Local: - allLabels = boxModel.AddBoxSize(labels, size).AddLocal(info.Path) - case template.Git: - allLabels = boxModel.AddBoxSize(labels, size).AddGit(info.Path, info.Revision) - } + allLabels := commonCmd.AddTemplateLabels[boxModel.BoxV1](info, boxModel.AddBoxSize(labels, size)) return &boxModel.CreateOptions{ Template: &info.Value.Data, diff --git a/internal/command/common/labels.go b/internal/command/common/labels.go new file mode 100644 index 00000000..24ffa928 --- /dev/null +++ b/internal/command/common/labels.go @@ -0,0 +1,17 @@ +package common + +import ( + commonModel "github.com/hckops/hckctl/pkg/common/model" + "github.com/hckops/hckctl/pkg/template" +) + +func AddTemplateLabels[T template.TemplateType](info *template.TemplateInfo[T], labels commonModel.Labels) commonModel.Labels { + var allLabels commonModel.Labels + switch info.SourceType { + case template.Local: + allLabels = labels.AddLocal(info.Path) + case template.Git: + allLabels = labels.AddGit(info.Path, info.Revision) + } + return allLabels +} diff --git a/internal/command/lab/lab.go b/internal/command/lab/lab.go index b9f37ce9..bd908b0a 100644 --- a/internal/command/lab/lab.go +++ b/internal/command/lab/lab.go @@ -72,8 +72,7 @@ func (opts *labCmdOptions) run(cmd *cobra.Command, args []string) error { log.Debug().Msgf("create lab from local template: path=%s", path) sourceLoader := template.NewLocalCachedLoader[labModel.LabV1](path, opts.configRef.Config.Template.CacheDir) - // TODO labels - return startLab(sourceLoader, opts.provider, opts.configRef) + return startLab(sourceLoader, opts.provider, opts.configRef, labModel.NewLabLabels().AddDefaultLocal()) } else { name := args[0] @@ -81,24 +80,24 @@ func (opts *labCmdOptions) run(cmd *cobra.Command, args []string) error { sourceOpts := commonCmd.NewGitSourceOptions(opts.configRef.Config.Template.CacheDir, opts.sourceFlag.Revision) sourceLoader := template.NewGitLoader[labModel.LabV1](sourceOpts, name) - // TODO labels - return startLab(sourceLoader, opts.provider, opts.configRef) + labels := labModel.NewLabLabels().AddDefaultGit(sourceOpts.RepositoryUrl, sourceOpts.DefaultRevision, sourceOpts.CacheDirName()) + return startLab(sourceLoader, opts.provider, opts.configRef, labels) } } -func startLab(sourceLoader template.SourceLoader[labModel.LabV1], provider labModel.LabProvider, configRef *config.ConfigRef) error { +func startLab(sourceLoader template.SourceLoader[labModel.LabV1], provider labModel.LabProvider, configRef *config.ConfigRef, labels commonModel.Labels) error { - labTemplate, err := sourceLoader.Read() - if err != nil || labTemplate.Value.Kind != schema.KindLabV1 { + info, err := sourceLoader.Read() + if err != nil || info.Value.Kind != schema.KindLabV1 { log.Warn().Err(err).Msg("error reading template") return errors.New("invalid template") } loader := commonCmd.NewLoader() - loader.Start("loading template %s", labTemplate.Value.Data.Name) + loader.Start("loading template %s", info.Value.Data.Name) defer loader.Stop() - log.Info().Msgf("loading template: provider=%s name=%s\n%s", provider, labTemplate.Value.Data.Name, labTemplate.Value.Data.Pretty()) + log.Info().Msgf("loading template: provider=%s name=%s\n%s", provider, info.Value.Data.Name, info.Value.Data.Pretty()) labClient, err := newDefaultLabClient(provider, configRef, loader) if err != nil { @@ -106,11 +105,12 @@ func startLab(sourceLoader template.SourceLoader[labModel.LabV1], provider labMo } createOpts := &labModel.CreateOptions{ - LabTemplate: &labTemplate.Value.Data, + LabTemplate: &info.Value.Data, BoxTemplates: map[string]*boxModel.BoxV1{}, // TODO load box templates DumpTemplates: map[string]*labModel.DumpV1{}, // TODO load dump templates Parameters: commonModel.Parameters{}, // TODO add overrides --input alias=parrot --input password=changeme --input vpn=htb-eu Labels: commonModel.Labels{}, // TODO box+lab labels + //Labels: commonCmd.AddTemplateLabels[labModel.LabV1](info, labels), } if labInfo, err := labClient.Create(createOpts); err != nil { diff --git a/internal/command/task/task.go b/internal/command/task/task.go index fed61c81..71e3715b 100644 --- a/internal/command/task/task.go +++ b/internal/command/task/task.go @@ -12,8 +12,8 @@ import ( "github.com/hckops/hckctl/internal/command/config" taskFlag "github.com/hckops/hckctl/internal/command/task/flag" commonModel "github.com/hckops/hckctl/pkg/common/model" + "github.com/hckops/hckctl/pkg/schema" "github.com/hckops/hckctl/pkg/task" - "github.com/hckops/hckctl/pkg/task/model" taskModel "github.com/hckops/hckctl/pkg/task/model" "github.com/hckops/hckctl/pkg/template" ) @@ -26,6 +26,8 @@ type taskCmdOptions struct { commandFlag string // e.g. default (nothing), inline (reserved keyword), other values } +// TODO offline + func NewTaskCmd(configRef *config.ConfigRef) *cobra.Command { opts := taskCmdOptions{ @@ -35,7 +37,6 @@ func NewTaskCmd(configRef *config.ConfigRef) *cobra.Command { command := &cobra.Command{ Use: "task [name]", Short: "Run a task", - Args: cobra.ExactArgs(1), PreRunE: opts.validate, RunE: opts.run, Hidden: false, // TODO WIP @@ -70,25 +71,30 @@ func (opts *taskCmdOptions) run(cmd *cobra.Command, args []string) error { path := args[0] log.Debug().Msgf("run task from local template: path=%s", path) - sourceLoader := template.NewLocalCachedLoader[model.TaskV1](path, opts.configRef.Config.Template.CacheDir) - // TODO labels - return runTask(sourceLoader, opts.provider, opts.configRef) + sourceLoader := template.NewLocalCachedLoader[taskModel.TaskV1](path, opts.configRef.Config.Template.CacheDir) + return runTask(sourceLoader, opts.provider, opts.configRef, taskModel.NewTaskLabels().AddDefaultLocal()) } else { name := args[0] log.Debug().Msgf("run task from git template: name=%s revision=%s", name, opts.sourceFlag.Revision) sourceOpts := commonCmd.NewGitSourceOptions(opts.configRef.Config.Template.CacheDir, opts.sourceFlag.Revision) - sourceLoader := template.NewGitLoader[model.TaskV1](sourceOpts, name) - // TODO labels - return runTask(sourceLoader, opts.provider, opts.configRef) + sourceLoader := template.NewGitLoader[taskModel.TaskV1](sourceOpts, name) + labels := taskModel.NewTaskLabels().AddDefaultGit(sourceOpts.RepositoryUrl, sourceOpts.DefaultRevision, sourceOpts.CacheDirName()) + return runTask(sourceLoader, opts.provider, opts.configRef, labels) } } -func runTask(sourceLoader template.SourceLoader[model.TaskV1], provider taskModel.TaskProvider, configRef *config.ConfigRef) error { +func runTask(sourceLoader template.SourceLoader[taskModel.TaskV1], provider taskModel.TaskProvider, configRef *config.ConfigRef, labels commonModel.Labels) error { + + info, err := sourceLoader.Read() + if err != nil || info.Value.Kind != schema.KindTaskV1 { + log.Warn().Err(err).Msg("error reading template") + return errors.New("invalid template") + } loader := commonCmd.NewLoader() - // TODO loader.Start("loading template %s", labTemplate.Value.Data.Name) + loader.Start("loading template %s", info.Value.Data.Name) defer loader.Stop() taskClient, err := newDefaultTaskClient(provider, configRef, loader) @@ -97,9 +103,9 @@ func runTask(sourceLoader template.SourceLoader[model.TaskV1], provider taskMode } createOpts := &taskModel.CreateOptions{ - TaskTemplate: nil, // TODO + TaskTemplate: &info.Value.Data, Parameters: commonModel.Parameters{}, // TODO common model - Labels: commonModel.Labels{}, // TODO + Labels: commonCmd.AddTemplateLabels[taskModel.TaskV1](info, labels), } return taskClient.Run(createOpts) diff --git a/pkg/lab/model/labels.go b/pkg/lab/model/labels.go new file mode 100644 index 00000000..7b140057 --- /dev/null +++ b/pkg/lab/model/labels.go @@ -0,0 +1,12 @@ +package model + +import ( + commonModel "github.com/hckops/hckctl/pkg/common/model" + "github.com/hckops/hckctl/pkg/schema" +) + +func NewLabLabels() commonModel.Labels { + return map[string]string{ + commonModel.LabelSchemaKind: schema.KindLabV1.String(), + } +} diff --git a/pkg/task/model/labels.go b/pkg/task/model/labels.go new file mode 100644 index 00000000..7a2ed207 --- /dev/null +++ b/pkg/task/model/labels.go @@ -0,0 +1,12 @@ +package model + +import ( + commonModel "github.com/hckops/hckctl/pkg/common/model" + "github.com/hckops/hckctl/pkg/schema" +) + +func NewTaskLabels() commonModel.Labels { + return map[string]string{ + commonModel.LabelSchemaKind: schema.KindTaskV1.String(), + } +}