2019-07-21 02:00:04 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-08-03 00:25:22 +00:00
|
|
|
|
|
|
|
"github.com/greenled/portainer-stack-utils/client"
|
2019-07-21 02:00:04 +00:00
|
|
|
)
|
|
|
|
|
2019-08-03 00:25:22 +00:00
|
|
|
func GetStackByName(name string) (stack client.Stack, err error) {
|
2019-08-02 16:03:43 +00:00
|
|
|
client, err := GetClient()
|
2019-07-21 02:00:04 +00:00
|
|
|
if err != nil {
|
2019-08-02 16:03:43 +00:00
|
|
|
return
|
2019-07-21 02:00:04 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 17:10:59 +00:00
|
|
|
stacks, err := client.GetStacks("", 0)
|
2019-07-21 02:00:04 +00:00
|
|
|
if err != nil {
|
2019-08-02 17:10:59 +00:00
|
|
|
return
|
2019-07-21 02:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, stack := range stacks {
|
|
|
|
if stack.Name == name {
|
|
|
|
return stack, nil
|
|
|
|
}
|
|
|
|
}
|
2019-08-02 17:10:59 +00:00
|
|
|
err = &StackNotFoundError{
|
2019-07-21 02:00:04 +00:00
|
|
|
StackName: name,
|
|
|
|
}
|
2019-08-02 17:10:59 +00:00
|
|
|
return
|
2019-07-21 02:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Custom customerrors
|
|
|
|
type StackNotFoundError struct {
|
|
|
|
StackName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *StackNotFoundError) Error() string {
|
|
|
|
return fmt.Sprintf("Stack %s not found", e.StackName)
|
|
|
|
}
|