Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions internal/handlers/descriptors/adapters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ export:
description: |
Export an adapter configuration

load:
use: adapters <path>
group: admin-essentials
description: |
Load all adapters

dump:
use: adapters
group: admin-essentials
description: |
Dump all adapters

#############################################################################
# Platform commands
#############################################################################
Expand Down
17 changes: 8 additions & 9 deletions internal/handlers/descriptors/workflows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,17 @@ export:


##############################################################################
# Gitter Commands
# Dataset Commands
##############################################################################
push:
use: workflow <name> <repository>
group: automation-studio

load:
use: workflows <path>
group: automation-studio
description: |
Push a workflow directly to a repository
Load workflows

pull:
use: workflow <filename> <repository>
dump:
use: workflows
group: automation-studio

description: |
Pull a workflow directly from a repository
Dump all workflows
5 changes: 1 addition & 4 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,10 @@ func NewHandler(iapClient client.Client, cfg *config.Config) Handler {

NewServerHandler(iapClient, cfg, descriptors),

NewLocalAAAHandler(iapClient, cfg, descriptors),
NewLocalClientHandler(iapClient, cfg, descriptors),
)

if cfg.MongoUri != "" {
NewLocalAAAHandler(iapClient, cfg, descriptors)
}

return Handler{
Runtime: &Runtime{
Config: cfg,
Expand Down
75 changes: 75 additions & 0 deletions internal/runners/adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,81 @@ func (r *AdapterRunner) Restart(in Request) (*Response, error) {
), nil
}

//////////////////////////////////////////////////////////////////////////////
// Dumper Interface
//

// Dump implements the `dump adapters...` command
func (r *AdapterRunner) Dump(in Request) (*Response, error) {
logger.Trace()

res, err := r.service.GetAll()
if err != nil {
return nil, err
}

var assets = map[string]interface{}{}
for _, ele := range res {
key := fmt.Sprintf("%s.adapter.json", ele.Name)
assets[key] = ele
}

if err := dumpAssets(in, assets); err != nil {
return nil, err
}

return NewResponse(
fmt.Sprintf("Dumped %v adapter(s)", len(assets)),
), nil
}

//////////////////////////////////////////////////////////////////////////////
// Loader Interface
//

// Load implements the `load adapters ...` command
func (r *AdapterRunner) Load(in Request) (*Response, error) {
logger.Trace()

elements, err := loadAssets(in)
if err != nil {
return nil, err
}

var loaded int
var skipped int

var output []string

for fn, ele := range elements {
var adapter services.Adapter

if err := loadUnmarshalAsset(ele, &adapter); err != nil {
output = append(output, fmt.Sprintf("Failed to load adapter from `%s`, skipping", fn))
skipped++
} else {
if _, err := r.service.Import(adapter); err != nil {
if !strings.HasSuffix(err.Error(), "already exists!\"") {
return nil, err
}
output = append(output, fmt.Sprintf("Skipping `%s`, adapter `%s` already exists", fn, adapter.Name))
skipped++
} else {
output = append(output, fmt.Sprintf("Loaded adapter `%s` successfully from `%s`", adapter.Name, fn))
loaded++
}
}
}

output = append(output, fmt.Sprintf(
"\nSuccessfully loaded %v and skipped %v files from `%s`", loaded, skipped, in.Args[0],
))

return NewResponse(
strings.Join(output, "\n"),
), nil
}

//////////////////////////////////////////////////////////////////////////////
// Private functions
//
Expand Down
9 changes: 1 addition & 8 deletions internal/runners/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,5 @@ import (
// value must be the object instance.
func dumpAssets(in Request, objects map[string]interface{}) error {
logger.Trace()

for key, value := range objects {
if err := exportAssetFromRequest(in, value, key); err != nil {
return err
}
}

return nil
return exportAssets(in, objects)
}
87 changes: 0 additions & 87 deletions internal/runners/export.go

This file was deleted.

77 changes: 58 additions & 19 deletions internal/runners/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,70 @@ package runners
import (
"os"
"path/filepath"
"strings"

"github.com/itential/ipctl/internal/flags"
"github.com/itential/ipctl/internal/utils"
"github.com/itential/ipctl/pkg/logger"
giturls "github.com/whilp/git-urls"
)

// exportNewRepository will create a new Repository object from an export
// request.
func exportNewRepositoryFromRequest(in Request) *Repository {
common := in.Common.(*flags.AssetExportCommon)
// exportNewRepository will create a new Repository object from an the incoming
// Request object.
func exportNewRepositoryFromRequest(in Request) (*Repository, error) {
logger.Trace()

common := in.Common.(flags.Gitter)

url := common.GetRepository()
privateKeyFile := common.GetPrivateKeyFile()
reference := common.GetReference()

u, err := giturls.Parse(common.GetRepository())
if err != nil {
panic(err)
}

if u.Scheme == "file" && strings.HasPrefix(u.Path, "@") {
r, err := in.Config.GetRepository(u.Path[1:])
if err != nil {
return nil, err
}

url = r.Url

if privateKeyFile == "" {
privateKeyFile = r.PrivateKeyFile
}

if reference == "" {
reference = r.Reference
}
}

return NewRepository(
common.Repository,
WithReference(common.Reference),
WithPrivateKeyFile(common.PrivateKeyFile),
url,
WithReference(reference),
WithPrivateKeyFile(privateKeyFile),
WithName(in.Config.GitName),
WithEmail(in.Config.GitEmail),
)
), nil
}

// exportAssetFromRequest will take a request object and instance of an asset
// and write it to disk. If the Git command line options where invoked, it
// will write the asset to the repository and commit it. If not, this function
// will simply write the asset to the local disk.
func exportAssetFromRequest(in Request, o any, fn string) error {
return exportAssets(in, map[string]interface{}{fn: o})
}

// exportAssets accepts the Request object and a map of the assets and will
// write them to disk. If the request object includes repository settings,
// this function will push the assets into the repository. The assets argument
// must be a map where the key is the filename and the value is the asset to
// write to disk.
func exportAssets(in Request, assets map[string]interface{}) error {
logger.Trace()

path := in.Common.(flags.Committer).GetPath()
Expand All @@ -39,16 +79,13 @@ func exportAssetFromRequest(in Request, o any, fn string) error {
var repoPath string

if in.Common.(flags.Gitter).GetRepository() != "" {
repo = NewRepository(
in.Common.(flags.Gitter).GetRepository(),
WithReference(in.Common.(flags.Gitter).GetReference()),
WithPrivateKeyFile(in.Common.(flags.Gitter).GetPrivateKeyFile()),
WithName(in.Config.GitName),
WithEmail(in.Config.GitEmail),
)

var e error

repo, e = exportNewRepositoryFromRequest(in)
if e != nil {
return e
}

repoPath, e = repo.Clone()
if e != nil {
return e
Expand All @@ -58,11 +95,13 @@ func exportAssetFromRequest(in Request, o any, fn string) error {
path = filepath.Join(repoPath, in.Common.(flags.Committer).GetPath())
}

if err := utils.WriteJsonToDisk(o, fn, path); err != nil {
return err
for key, value := range assets {
if err := utils.WriteJsonToDisk(value, key, path); err != nil {
return err
}
}

if in.Common.(flags.Gitter).GetRepository() != "" {
if repo != nil {
msg := in.Common.(flags.Committer).GetMessage()
if err := repo.CommitAndPush(repoPath, msg); err != nil {
return err
Expand Down
5 changes: 4 additions & 1 deletion internal/runners/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,10 @@ func (r *ModelRunner) Export(in Request) (*Response, error) {
var repoPath string

if common.Repository != "" {
repo = exportNewRepositoryFromRequest(in)
repo, err = exportNewRepositoryFromRequest(in)
if err != nil {
return nil, err
}

var e error

Expand Down
5 changes: 4 additions & 1 deletion internal/runners/prebuilts.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,10 @@ func (r *PrebuiltRunner) Export(in Request) (*Response, error) {
var repoPath string

if common.Repository != "" {
repo = exportNewRepositoryFromRequest(in)
repo, err = exportNewRepositoryFromRequest(in)
if err != nil {
return nil, err
}

var e error

Expand Down
5 changes: 4 additions & 1 deletion internal/runners/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,10 @@ func (r *ProjectRunner) Export(in Request) (*Response, error) {
var repoPath string

if common.Repository != "" {
repo = exportNewRepositoryFromRequest(in)
repo, err = exportNewRepositoryFromRequest(in)
if err != nil {
return nil, err
}

var e error

Expand Down
Loading
Loading