Golang SDK
- Updated On 18 Nov 2020
- 3 Minutes To Read
-
DarkLight
The Golang version of The Ops Platform SDK works in conjunction with our Docker base images. The SDK allows the development of automations with rich user interfaces, that run in both the local terminal and in our Slack App as Slack Ops.
Supported methods
The following SDK methods are supported in Golang:
- GetHostOS
- GetInterfaceType
- GetSecret
- GetStatePath
- HomeDir
- SetSecret
- Track
- GetState
- SetState
- GetConfig
- GetAllConfig
- SetConfig
To use the SDK, you need to create a new SDK object:
func NewSdk() *Sdk
Alternatively, the SDK object is included in the Client object:
client := ctoai.NewClient()
func (*Sdk) GetHostOS
func (*Sdk) GetHostOS() string
Returns the current host OS.
Example
package main
import (
ctoai "github.com/cto-ai/sdk-go"
)
func main() {
client := ctoai.NewClient()
o := client.Sdk.GetHostOS()
client.Ux.Print(o)
}
Output
darwin
func (*Sdk) GetInterfaceType
func (*Sdk) GetInterfaceType() string
Returns the interface type that the Op is attached to (e.g. terminal or Slack).
Example
package main
import (
ctoai "github.com/cto-ai/sdk-go"
)
func main() {
client := ctoai.NewClient()
o := client.Sdk.GetInterfaceType()
client.Ux.Print(o)
}
Output
terminal
func (*Sdk) GetSecret
func (*Sdk) GetSecret(key string, options ...GetSecretOption) (string, error)
Requests a secret from the secret store by key
.
If the secret exists, it is returned. A notification is printed to the user that the secret has been accessed, unless the OptGetSecretHidden
option is passed.
If the secret does not exist, the user is prompted to provide a replacement, either from the secret store or by direct entry through their interface.
Example
package main
import (
ctoai "github.com/cto-ai/sdk-go"
)
func main() {
client := ctoai.NewClient()
o, err := client.Sdk.GetSecret("sec1")
client.Ux.Print(o)
if err != nil {
panic(err)
}
}
Output
Failed to retrieve secret sec1!
We were unable to find a secret with key sec1. Please select an alternate key or enter a value.
Enter a value:
func (*Sdk) HomeDir
func (*Sdk) HomeDir() string
Returns the location of the user home directory.
Example
package main
import (
ctoai "github.com/cto-ai/sdk-go"
)
func main() {
client := ctoai.NewClient()
o := client.Sdk.HomeDir()
client.Ux.Print(o)
}
Output
/root
func (*Sdk) SetSecret
func (*Sdk) SetSecret(key string, value string) (string, error)
Sets a particular value into the secret store.
If the secret already exists, the user is prompted on whether to overwrite it.
Example
package main
import (
ctoai "github.com/cto-ai/sdk-go"
)
func main() {
client := ctoai.NewClient()
s,err := client.Sdk.SetSecret("sec1", "password1")
client.Ux.Print(s)
if err != nil {
panic(err)
}
}
Output
sec1
func (*Sdk) Track
The track function allows you to store and retrieve custom analytic data.
func (*Sdk) Track(tags []string, event string, metadata map[string]interface{}) error
The
tags
field is deprecated. Please only pass[]string{}
as the tags argument, and do not use this field for data.
Example
s := ctoai.NewSdk()
err := s.Track([]string{}, "testing", map[string]interface{}{
"user": "name",
})
Using Track for Workflow Metrics
Tracking Workflow Metrics using CTO.ai can give deep insight into your team's activity.
To use track to send a Workflow Metric event, place your event data in the metadata
field like so:
package main
import (
ctoai "github.com/cto-ai/sdk-go"
)
func main() {
client := ctoai.NewClient()
event := map[string]interface{}{
"stage": "Deployment",
"status": "Succeeded",
"stage_ref": "956b425e5bee",
"pipeline_id": "web",
}
err := client.Sdk.Track([]string{}, "", event)
if err != nil {
panic(err)
}
}
The first two arguments to client.Sdk.Track
are left blank because they required by Track
, but are not used by the metrics dashboard.
More information about Workflow Metrics can be found on the Workflow Metrics overview page.
func (*Sdk) Events
func (*Sdk) Events(starttime string, endtime string) ([]map[string]interface{}, error)
Retrieves events to the CTO.ai analytics system.
This is the public facing API to enable developers to retrieve workflow events.
Example
s := ctoai.NewSdk()
err := s.Events("2020-05-12T20:47:45Z", "2020-06-12T20:47:45Z")
The event, tags, and payload will be retrieved from the events system.
func (s *Sdk) GetConfig
func (s *Sdk) GetConfig(key string) (string, error)
Returns a value from the team configuration store. If the value does not exist, returns an empty string.
Example
s := ctoai.NewSdk()
configValue, err := s.GetConfig("relevant_configuration")
func (s *Sdk) GetAllConfig
func (s *Sdk) GetAllConfig() (map[string]string, error)
Returns the contents of the team configuration store, as a map of keys to values.
Example
s := ctoai.NewSdk()
fullConfig, err := s.GetAllConfig()
func (s *Sdk) SetConfig
func (s *Sdk) SetConfig(key string, value string) error
Sets a value in the team configuration store.
Example
s := ctoai.NewSdk()
err := s.SetConfig("AWS_PROFILE", "default")
func (s *Sdk) DeleteConfig
func (s *Sdk) DeleteConfig(key string) (bool, error)
Deletes a key-value pair in the persistent team configuration. Returns true
if the key exists and the deletion is successful, or false
if the key cannot be found.
Example
s := ctoai.NewSdk()
wasDeleted, err := s.DeleteConfig("AWS_PROFILE")
🚀 What's next?
- To learn more about the UX options available for you, please go to Golang UX page.
- To see which prompts we offer for this SDK, you can go to Golang Prompts page.