Golang SDK
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) (string, error)
Requests a secret from the secret store by key
.
If the secret exists, it is returned, with the daemon notifying the user that it is in use.
Otherwise, the user is prompted to provide a replacement.
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
func (*Sdk) Track(tags []string, event string, metadata map[string]interface{}) error
Sends an event to the CTO.ai workflow event system.
This is the public facing API to enable developers to send workflow events.
Example
s := ctoai.NewSdk()
err := s.Track([]string{"sdk", "go", "tracked"}, "testing", map[string]interface{}{
"user": "name",
})
The event, tags, and payload will be sent to the events system.
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.
- Add Secrets Management page to the Password and Secrets prompts.