airshipui/pkg/ctl/document.go
Matthew Fuller fab7bd9ef5 Add config integration to airshipui
Integrates airshipctl's config functionality with
Airship UI to allow users to view and set airship
configuration settings.

Known issues:
- Manifests currently only shows the primary (phase)
  repo. We'll probably need a separate repo sub-component
  to allow for showing / editing multiple repos
- There are some boolean values which once set, cannot
  be unset using airshipctl's setters. We may need to
  write custom setters to set the Config struct values
  directly
- It's possible to make edits to the config file that
  render the config invalid, so the CTL client cannot
  be initialized for subsequent edits. We'll probably
  want to make a copy of the original config, test the
  changes by initializing a new client, and only persist
  the changes if valid.
- Lots and lots of cosmetic work remains to make the
  output more readable and easier to manage

Change-Id: Ib29f3f6cf3e420b6e0e2cdc6afddd48c7e403137
2020-10-29 20:35:10 +00:00

76 lines
1.9 KiB
Go

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ctl
import (
"fmt"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/document/pull"
"opendev.org/airship/airshipui/pkg/configs"
)
// HandleDocumentRequest will flop between requests so we don't have to have them all mapped as function calls
func HandleDocumentRequest(user *string, request configs.WsMessage) configs.WsMessage {
response := configs.WsMessage{
Type: configs.CTL,
Component: configs.Document,
SubComponent: request.SubComponent,
}
var err error
var message *string
client, err := NewClient(AirshipConfigPath, KubeConfigPath, request)
if err != nil {
e := err.Error()
response.Error = &e
return response
}
subComponent := request.SubComponent
switch subComponent {
case configs.Pull:
message, err = client.docPull()
case configs.Plugin:
err = fmt.Errorf("Subcomponent %s not implemented", request.SubComponent)
default:
err = fmt.Errorf("Subcomponent %s not found", request.SubComponent)
}
if err != nil {
e := err.Error()
response.Error = &e
} else {
response.Message = message
}
return response
}
func (c *Client) docPull() (*string, error) {
var message *string
cfgFactory := config.CreateFactory(AirshipConfigPath)
// 2nd arg is noCheckout, I assume we want to checkout the repo,
// so setting to false
err := pull.Pull(cfgFactory, false)
if err == nil {
s := "Success"
message = &s
}
return message, err
}