Skip to content
This repository has been archived by the owner on Sep 28, 2021. It is now read-only.

Commit

Permalink
Add alternate states (#394)
Browse files Browse the repository at this point in the history
* Adding possbility to add alternate states

* Adding alternate states doc

* Adding spec for alternate states

* Update golden files for help (command added)

* Adding doc

* Add tests

* Fixes after review

* Update after review

* And update spec

* Setting API to experimental

* Removed superfluous code

* Fixed typo, changed to plurarl, testing remove as well
  • Loading branch information
axelssonHakan committed Sep 3, 2019
1 parent 0dcc4cd commit 420e67b
Show file tree
Hide file tree
Showing 15 changed files with 364 additions and 3 deletions.
77 changes: 77 additions & 0 deletions cmd/alternate_state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package cmd

import (
"github.com/qlik-oss/corectl/internal"
"github.com/qlik-oss/corectl/printer"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var listAlternateStatesCmd = &cobra.Command{
Use: "ls",
Args: cobra.ExactArgs(0),
Short: "Print a list of all alternate states in the current app",
Long: "Print a list of all alternate states in the current app",
Example: "corectl state ls",

Run: func(ccmd *cobra.Command, args []string) {
state := internal.PrepareEngineState(rootCtx, headers, certificates, false)
items := internal.ListAlternateStates(state.Ctx, state.Doc)
printer.PrintStates(items, viper.GetBool("bash"))
},
}

var addAlternateStateCmd = &cobra.Command{
Use: "add <alternate-state-name>",
Args: cobra.ExactArgs(1),
Short: "Add an alternate states in the current app",
Long: "Add an alternate states in the current app",
Example: "corectl state add NAME-1",

Run: func(ccmd *cobra.Command, args []string) {
stateName := args[0]
if stateName == "" {
internal.FatalError("no state name specified")
}
state := internal.PrepareEngineState(rootCtx, headers, certificates, false)
internal.AddAlternateState(state.Ctx, state.Doc, stateName)
if !viper.GetBool("no-save") {
internal.Save(rootCtx, state.Doc)
}
},
}

var removeAlternateStateCmd = &cobra.Command{
Use: "rm <alternate-state-name>",
Args: cobra.ExactArgs(1),
Short: "Removes an alternate state in the current app",
Long: "Removes an alternate state in the current app",
Example: "corectl state rm NAME-1",

Run: func(ccmd *cobra.Command, args []string) {
stateName := args[0]
if stateName == "" {
internal.FatalError("no state name specified")
}
state := internal.PrepareEngineState(rootCtx, headers, certificates, false)
internal.RemoveAlternateState(state.Ctx, state.Doc, stateName)
if !viper.GetBool("no-save") {
internal.Save(rootCtx, state.Doc)
}
},
}

var alternateStateCmd = &cobra.Command{
Use: "state",
Short: "Explore and manage alternate states",
Long: "Explore and manage alternate states",
Aliases: []string{"alternatestate"},
Annotations: map[string]string{
"command_category": "sub",
"x-qlik-stability": "experimental",
},
}

func init() {
alternateStateCmd.AddCommand(listAlternateStatesCmd, addAlternateStateCmd, removeAlternateStateCmd)
}
8 changes: 8 additions & 0 deletions cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ __corectl_custom_func()
corectl_connection_get | corectl_connection_rm)
__corectl_get_connections
;;
corectl_state_rm)
__corectl_state_ls
;;
corectl_app_rm)
__corectl_get_apps
;;
Expand Down Expand Up @@ -173,6 +176,11 @@ __corectl_get_connections()
__corectl_call_corectl "connection ls --bash"
}
__corectl_state_ls()
{
__corectl_call_corectl "state ls --bash"
}
__corectl_get_apps()
{
__corectl_call_corectl "app ls --bash"
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func init() {
rootCmd.AddCommand(unbuildCmd)

// Subcommands
rootCmd.AddCommand(alternateStateCmd)
rootCmd.AddCommand(measureCmd)
rootCmd.AddCommand(dimensionCmd)
rootCmd.AddCommand(objectCmd)
Expand Down
1 change: 1 addition & 0 deletions docs/corectl.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ corectl [flags]
* [corectl object](corectl_object.md) - Explore and manage generic objects
* [corectl reload](corectl_reload.md) - Reload and save the app
* [corectl script](corectl_script.md) - Explore and manage the script
* [corectl state](corectl_state.md) - Explore and manage alternate states
* [corectl status](corectl_status.md) - Print status info about the connection to the engine and current app
* [corectl tables](corectl_tables.md) - Print tables
* [corectl unbuild](corectl_unbuild.md) - Split up an existing app into separate json and yaml files
Expand Down
37 changes: 37 additions & 0 deletions docs/corectl_state.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## corectl state

Explore and manage alternate states

### Synopsis

Explore and manage alternate states

### Options

```
-h, --help help for state
```

### Options inherited from parent commands

```
-a, --app string Name or identifier of the app
--certificates string path/to/folder containing client.pem, client_key.pem and root.pem certificates
-c, --config string path/to/config.yml where parameters can be set instead of on the command line
--context string Name of the context used when connecting to Qlik Associative Engine
-e, --engine string URL to the Qlik Associative Engine (default "localhost:9076")
--headers stringToString Http headers to use when connecting to Qlik Associative Engine (default [])
--json Returns output in JSON format if possible, disables verbose and traffic output
--no-data Open app without data
-t, --traffic Log JSON websocket traffic to stdout
--ttl string Qlik Associative Engine session time to live in seconds (default "0")
-v, --verbose Log extra information
```

### SEE ALSO

* [corectl](corectl.md) -
* [corectl state add](corectl_state_add.md) - Add an alternate states in the current app
* [corectl state ls](corectl_state_ls.md) - Print a list of all alternate states in the current app
* [corectl state rm](corectl_state_rm.md) - Removes an alternate state in the current app

44 changes: 44 additions & 0 deletions docs/corectl_state_add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
## corectl state add

Add an alternate states in the current app

### Synopsis

Add an alternate states in the current app

```
corectl state add <alternate-state-name> [flags]
```

### Examples

```
corectl state add NAME-1
```

### Options

```
-h, --help help for add
```

### Options inherited from parent commands

```
-a, --app string Name or identifier of the app
--certificates string path/to/folder containing client.pem, client_key.pem and root.pem certificates
-c, --config string path/to/config.yml where parameters can be set instead of on the command line
--context string Name of the context used when connecting to Qlik Associative Engine
-e, --engine string URL to the Qlik Associative Engine (default "localhost:9076")
--headers stringToString Http headers to use when connecting to Qlik Associative Engine (default [])
--json Returns output in JSON format if possible, disables verbose and traffic output
--no-data Open app without data
-t, --traffic Log JSON websocket traffic to stdout
--ttl string Qlik Associative Engine session time to live in seconds (default "0")
-v, --verbose Log extra information
```

### SEE ALSO

* [corectl state](corectl_state.md) - Explore and manage alternate states

44 changes: 44 additions & 0 deletions docs/corectl_state_ls.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
## corectl state ls

Print a list of all alternate states in the current app

### Synopsis

Print a list of all alternate states in the current app

```
corectl state ls [flags]
```

### Examples

```
corectl state ls
```

### Options

```
-h, --help help for ls
```

### Options inherited from parent commands

```
-a, --app string Name or identifier of the app
--certificates string path/to/folder containing client.pem, client_key.pem and root.pem certificates
-c, --config string path/to/config.yml where parameters can be set instead of on the command line
--context string Name of the context used when connecting to Qlik Associative Engine
-e, --engine string URL to the Qlik Associative Engine (default "localhost:9076")
--headers stringToString Http headers to use when connecting to Qlik Associative Engine (default [])
--json Returns output in JSON format if possible, disables verbose and traffic output
--no-data Open app without data
-t, --traffic Log JSON websocket traffic to stdout
--ttl string Qlik Associative Engine session time to live in seconds (default "0")
-v, --verbose Log extra information
```

### SEE ALSO

* [corectl state](corectl_state.md) - Explore and manage alternate states

44 changes: 44 additions & 0 deletions docs/corectl_state_rm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
## corectl state rm

Removes an alternate state in the current app

### Synopsis

Removes an alternate state in the current app

```
corectl state rm <alternate-state-name> [flags]
```

### Examples

```
corectl state rm NAME-1
```

### Options

```
-h, --help help for rm
```

### Options inherited from parent commands

```
-a, --app string Name or identifier of the app
--certificates string path/to/folder containing client.pem, client_key.pem and root.pem certificates
-c, --config string path/to/config.yml where parameters can be set instead of on the command line
--context string Name of the context used when connecting to Qlik Associative Engine
-e, --engine string URL to the Qlik Associative Engine (default "localhost:9076")
--headers stringToString Http headers to use when connecting to Qlik Associative Engine (default [])
--json Returns output in JSON format if possible, disables verbose and traffic output
--no-data Open app without data
-t, --traffic Log JSON websocket traffic to stdout
--ttl string Qlik Associative Engine session time to live in seconds (default "0")
-v, --verbose Log extra information
```

### SEE ALSO

* [corectl state](corectl_state.md) - Explore and manage alternate states

16 changes: 16 additions & 0 deletions docs/spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,22 @@
}
}
},
"state": {
"alias": "alternatestate",
"description": "Explore and manage alternate states",
"x-qlik-stability": "experimental",
"commands": {
"add": {
"description": "Add an alternate states in the current app"
},
"ls": {
"description": "Print a list of all alternate states in the current app"
},
"rm": {
"description": "Removes an alternate state in the current app"
}
}
},
"status": {
"description": "Print status info about the connection to the engine and current app, and also the status of the data model"
},
Expand Down
42 changes: 42 additions & 0 deletions internal/alternate_state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package internal

import (
"context"

"github.com/qlik-oss/enigma-go"
)

// ListAlternateStates will return a list of all alternate states in an app.
func ListAlternateStates(ctx context.Context, doc *enigma.Doc) []string {
appLayout, _ := doc.GetAppLayout(ctx)
return appLayout.StateNames
}

// AddAlternateState will add a named alternate state in the app.
func AddAlternateState(ctx context.Context, doc *enigma.Doc, alternateStateName string) {
err := doc.AddAlternateState(ctx, alternateStateName)
if err != nil {
FatalErrorf("could not add state %s: %s ", alternateStateName, err)
}
}

// RemoveAlternateState will remove a named alternate state in the app.
func RemoveAlternateState(ctx context.Context, doc *enigma.Doc, alternateStateName string) {
states := ListAlternateStates(ctx, doc)
var stateNameExists bool
for _, state := range states {
if state == alternateStateName {
stateNameExists = true
break
}
}

if !stateNameExists {
FatalErrorf("no alternate state with the name '%s' found in the app", alternateStateName)
}

err := doc.RemoveAlternateState(ctx, alternateStateName)
if err != nil {
FatalErrorf("could not remove state %s: %s ", alternateStateName, err)
}
}
29 changes: 29 additions & 0 deletions printer/alternate_states.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package printer

import (
"os"

"github.com/qlik-oss/corectl/internal"

"github.com/olekukonko/tablewriter"
)

// PrintStates prints a list of states to system out.
func PrintStates(statesList []string, printAsBash bool) {
if internal.PrintJSON {
internal.PrintAsJSON(statesList)
} else if printAsBash {
for _, state := range statesList {
PrintToBashComp(state)
}
} else {
writer := tablewriter.NewWriter(os.Stdout)
writer.SetAutoFormatHeaders(false)
writer.SetHeader([]string{"Name"})

for _, state := range statesList {
writer.Append([]string{state})
}
writer.Render()
}
}
Loading

0 comments on commit 420e67b

Please sign in to comment.