Skip to content

Commit

Permalink
Add tutorial message template for x plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
mszostok committed Jul 9, 2023
1 parent 2872a2e commit 91929b0
Show file tree
Hide file tree
Showing 11 changed files with 528 additions and 86 deletions.
62 changes: 35 additions & 27 deletions cmd/executor/x/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (i *XExecutor) Execute(ctx context.Context, in executor.ExecuteInput) (exec
},
}
if err := pluginx.MergeExecutorConfigs(in.Configs, &cfg); err != nil {
return executor.ExecuteOutput{}, err
return executor.ExecuteOutput{}, fmt.Errorf("while merging configs: %v", err)
}

log := loggerx.New(cfg.Logger)
Expand All @@ -114,9 +114,10 @@ func (i *XExecutor) Execute(ctx context.Context, in executor.ExecuteInput) (exec
err = renderer.RegisterAll(map[string]x.Render{
"parser:table:.*": output.NewTableCommandParser(log),
"wrapper": output.NewCommandWrapper(),
"tutorial": output.NewTutorialWrapper(),
})
if err != nil {
return executor.ExecuteOutput{}, err
return executor.ExecuteOutput{}, fmt.Errorf("while registering message renderers: %v", err)
}

runner := x.NewRunner(log, renderer)
Expand All @@ -128,22 +129,25 @@ func (i *XExecutor) Execute(ctx context.Context, in executor.ExecuteInput) (exec
tool := Normalize(strings.Join(cmd.Run.Tool, " "))
log.WithField("tool", tool).Info("Running command...")

kubeConfigPath, deleteFn, err := i.getKubeconfig(ctx, log, in)
defer deleteFn()
if err != nil {
return executor.ExecuteOutput{}, err
}

command := x.Parse(tool)
out, err := x.RunInstalledCommand(ctx, cfg.TmpDir, command.ToExecute, map[string]string{
"KUBECONFIG": kubeConfigPath,
})
if err != nil {
log.WithError(err).WithField("command", command.ToExecute).Error("failed to run command")
return executor.ExecuteOutput{}, err
run := func() (string, error) {
kubeConfigPath, deleteFn, err := i.getKubeconfig(ctx, log, in)
defer deleteFn()
if err != nil {
return "", err
}

out, err := x.RunInstalledCommand(ctx, cfg.TmpDir, command.ToExecute, map[string]string{
"KUBECONFIG": kubeConfigPath,
})
if err != nil {
log.WithError(err).WithField("command", command.ToExecute).Error("failed to run command")
return "", err
}
return out, nil
}

return runner.Run(ctx, cfg, state, command, out)
return runner.Run(ctx, cfg, state, command, run)
case cmd.Install != nil:
var (
tool = Normalize(strings.Join(cmd.Install.Tool, " "))
Expand All @@ -152,20 +156,24 @@ func (i *XExecutor) Execute(ctx context.Context, in executor.ExecuteInput) (exec
downloadCmd = fmt.Sprintf("eget %s", tool)
)

log.WithFields(logrus.Fields{
"dir": dir,
"isCustom": isCustom,
"userCommand": command.ToExecute,
"runCommand": downloadCmd,
}).Info("Installing binary...")

if _, err := pluginx.ExecuteCommand(ctx, downloadCmd, pluginx.ExecuteCommandEnvs(map[string]string{
"EGET_BIN": dir,
})); err != nil {
return executor.ExecuteOutput{}, err
run := func() (string, error) {
log.WithFields(logrus.Fields{
"dir": dir,
"isCustom": isCustom,
"userCommand": command.ToExecute,
"runCommand": downloadCmd,
}).Info("Installing binary...")

if _, err := pluginx.ExecuteCommand(ctx, downloadCmd, pluginx.ExecuteCommandEnvs(map[string]string{
"EGET_BIN": dir,
})); err != nil {
return "", err
}

return "Binary was installed successfully 🎉", nil
}

return runner.Run(ctx, cfg, state, command, "Binary was installed successfully 🎉")
return runner.Run(ctx, cfg, state, command, run)
}
return executor.ExecuteOutput{
Message: api.NewPlaintextMessage("Command not supported", false),
Expand Down
19 changes: 10 additions & 9 deletions cmd/executor/x/templates/argo.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
templates:
- command:
prefix: "argo list"
parser: "table"
- trigger:
command:
prefix: "argo list"
type: "parser:table:space"
message:
select:
name: "Workflows"
itemKey: "{{ .Namespace }}/{{ .Name }}"
selects:
- name: "Workflows"
keyTpl: "{{ .Namespace }}/{{ .Name }}"
actions:
logs: "argo logs {{ .Name }} -n {{ .Namespace }}"
describe: "argo get {{ .Name }} -n {{ .Namespace }}"
delete: "argo delete {{ .Name }} -n {{ .Namespace }}"
logs: "argo logs {{ .Name }} -n {{ .Namespace }}"
describe: "argo get {{ .Name }} -n {{ .Namespace }}"
delete: "argo delete {{ .Name }} -n {{ .Namespace }}"
94 changes: 88 additions & 6 deletions cmd/executor/x/templates/flux.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
templates:
- trigger:
command: "flux get sources git"
command:
prefix: "flux get sources git"
type: "parser:table:space"
message:
selects:
Expand All @@ -16,12 +17,93 @@ templates:
Message: {{ .Message}}
- trigger:
command: "x install github.com/fluxcd/flux2"
command:
prefix: "x install github.com/fluxcd/flux2"
type: "wrapper"
message:
buttons:
- name: "Get Help"
command: "{{BotName}} x run flux --help"
- name: "Initialize"
command: "{{BotName}} x run flux install"
- name: "Quickstart"
command: "{{BotName}} x run quickstart flux"
style: "primary"

- trigger:
command:
prefix: "quickstart flux"
type: "tutorial"
message:
paginate:
page: 5
header: "Flux Quick Start tutorial"
buttons:
- name: "Check prerequisites"
command: "{{BotName}} x run flux check --pre"
description: "{{BotName}} flux check --pre"
- name: "Install Flux"
command: "{{BotName}} x run flux install"
description: "{{BotName}} flux install"
- name: "Create Git source"
command: |
{{BotName}} x run flux create source git webapp-latest
--url=https://github.com/stefanprodan/podinfo
--branch=master
--interval=3m
description: |
{{BotName}} flux create source git webapp-latest
--url=https://github.com/stefanprodan/podinfo
--branch=master
--interval=3m
- name: "List Git sources"
command: "{{BotName}} x run flux get sources git"
description: "{{BotName}} flux get sources git"
- name: "Reconcile Git source"
command: "{{BotName}} x run flux reconcile source git flux-system"
description: "{{BotName}} flux reconcile source git flux-system"
- name: "Export Git sources"
command: "{{BotName}} x run flux export source git --all"
description: "{{BotName}} flux export source git --all"
- name: "Create Kustomization"
command: |
{{BotName}} x run flux create kustomization webapp-dev
--source=webapp-latest
--path='./deploy/webapp/'
--prune=true
--interval=5m
--health-check='Deployment/backend.webapp'
--health-check='Deployment/frontend.webapp'
--health-check-timeout=2m
description: |
{{BotName}} x run flux create kustomization webapp-dev
--source=webapp-latest
--path='./deploy/webapp/'
--prune=true
--interval=5m
--health-check='Deployment/backend.webapp'
--health-check='Deployment/frontend.webapp'
--health-check-timeout=2m
- name: "Reconcile Kustomization"
command: "{{BotName}} x run flux reconcile kustomization webapp-dev --with-source"
description: "{{BotName}} flux reconcile kustomization webapp-dev --with-source"
- name: "Suspend Kustomization"
command: "{{BotName}} x run flux suspend kustomization webapp-dev"
description: "{{BotName}} flux suspend kustomization webapp-dev"
- name: "Export Kustomizations"
command: "{{BotName}} x run flux export kustomization --all"
description: "{{BotName}} flux export kustomization --all"
- name: "Resume Kustomization"
command: "{{BotName}} x run flux resume kustomization webapp-dev"
description: "{{BotName}} flux resume kustomization webapp-dev"
- name: "Delete Kustomization"
command: "{{BotName}} x run flux delete kustomization webapp-dev"
description: "{{BotName}} flux delete kustomization webapp-dev"
- name: "Delete Git source"
command: "{{BotName}} x run flux delete source git webapp-latest"
description: "{{BotName}} flux delete source"
- name: "Delete Kustomization"
command: "{{BotName}} x run flux delete kustomization webapp-dev"
description: "{{BotName}} flux delete kustomization webapp-dev"
- name: "Delete Git source"
command: "{{BotName}} x run flux delete source git webapp-latest --silent"
description: "{{BotName}} flux delete source git webapp-latest --silent"
- name: "Uninstall Flux"
command: "{{BotName}} x run flux uninstall"
description: "{{BotName}} flux uninstall"
55 changes: 53 additions & 2 deletions cmd/executor/x/templates/helm.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,68 @@
templates:
- trigger:
command: "helm list"
command:
regex: '^helm list(?:\s+(-A|-a))*\s?$'
type: "parser:table:space"
message:
selects:
- name: "Release"
keyTpl: "{{ .Namespace }}/{{ .Name }}"
actions:
notes: "helm get notes {{ .Name }} -n {{ .Namespace }}"
notes: "helm get notes {{ .Name }} -n {{ .Namespace }}"
values: "helm get values {{ .Name }} -n {{ .Namespace }}"
delete: "helm delete {{ .Name }} -n {{ .Namespace }}"
preview: |
Name: {{ .Name }}
Namespace: {{ .Namespace }}
Status: {{ .Status }}
Chart: {{ .Chart }}
- trigger:
command:
prefix: "x install https://get.helm.sh/helm-v"
type: "wrapper"
message:
buttons:
- name: "Quickstart"
command: "{{BotName}} x run quickstart helm"
style: "primary"

- trigger:
command:
prefix: "quickstart helm"
type: "tutorial"
message:
paginate:
page: 5
header: "Helm Quick Start tutorial"
buttons:
- name: "Global Help"
description: "{{BotName}} helm help"
command: "{{BotName}} x run helm help"
- name: "Version"
description: "{{BotName}} helm version"
command: "{{BotName}} x run helm version"
- name: "Install help"
description: "{{BotName}} helm install -h"
command: "{{BotName}} x run helm install -h"
- name: "Install by absolute URL"
description: "{{BotName}} helm install\n--repo https://charts.bitnami.com/bitnami psql postgresql\n--set clusterDomain='testing.local'"
command: "{{BotName}} x run helm install\n--repo https://charts.bitnami.com/bitnami psql postgresql\n--set clusterDomain='testing.local'"
- name: "Install by chart reference:"
description: "{{BotName}} helm install https://charts.bitnami.com/bitnami/postgresql-12.1.0.tgz --create-namespace -n test --generate-name"
command: "{{BotName}} x run helm install https://charts.bitnami.com/bitnami/postgresql-12.1.0.tgz --create-namespace -n test --generate-name"
- name: "List"
description: "{{BotName}} helm list -A"
command: "{{BotName}} x run helm list -A"
- name: "List with filter"
description: "{{BotName}} helm list -f 'p' -A"
command: "{{BotName}} x run helm list -f 'p' -A"
- name: "Status"
description: "{{BotName}} helm status psql"
command: "{{BotName}} x run helm status psql"
- name: "Upgrade"
description: "{{BotName}} helm upgrade --repo https://charts.bitnami.com/bitnami psql postgresql --set clusterDomain='cluster.local'"
command: "{{BotName}} x run helm upgrade --repo https://charts.bitnami.com/bitnami psql postgresql --set clusterDomain='cluster.local'"
- name: "History"
description: "{{BotName}} helm history psql"
command: "{{BotName}} x run helm history psql"
9 changes: 7 additions & 2 deletions internal/executor/x/getter/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package getter

import (
"context"
"fmt"
"io/fs"
"os"
"path/filepath"
Expand All @@ -16,6 +17,10 @@ type Source struct {

// Load downloads defined sources and read them from the FS.
func Load[T any](ctx context.Context, tmpDir string, templateSources []Source) ([]T, error) {
if len(templateSources) == 0 {
return nil, nil
}

err := EnsureDownloaded(ctx, templateSources, tmpDir)
if err != nil {
return nil, err
Expand All @@ -36,15 +41,15 @@ func Load[T any](ctx context.Context, tmpDir string, templateSources []Source) (

file, err := os.ReadFile(filepath.Clean(path))
if err != nil {
return err
return fmt.Errorf("while reading file %q: %v", path, err)
}

var cfg struct {
Templates []T `yaml:"templates"`
}
err = yaml.Unmarshal(file, &cfg)
if err != nil {
return err
return fmt.Errorf("while unmarshaling file %q: %v", path, err)
}
out = append(out, cfg.Templates...)
return nil
Expand Down
8 changes: 8 additions & 0 deletions internal/executor/x/mathx/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ func DecreaseWithMin(in, min int) int {
}
return in
}

// Max returns the largest of a or b.
func Max(a, b int) int {
if a > b {
return b
}
return a
}
Loading

0 comments on commit 91929b0

Please sign in to comment.