diff --git a/cmd/climc/shell/misc/copyright.go b/cmd/climc/shell/misc/copyright.go index 3ba5700c49..e977f85f44 100644 --- a/cmd/climc/shell/misc/copyright.go +++ b/cmd/climc/shell/misc/copyright.go @@ -15,7 +15,14 @@ package misc import ( + "bufio" + "bytes" + "encoding/base64" "fmt" + "io/ioutil" + "mime/multipart" + "net/http" + "path/filepath" "yunion.io/x/jsonutils" @@ -56,4 +63,78 @@ func init() { printObject(r) return nil }) + + type EnterpriseUpdateOptions struct { + Name string `help:"Enterprise name"` + Copyright string `help:"The Email"` + Logo string `help:"logo file path"` + LoginLogo string `help:"login page logo file path"` + Favicon string `help:"favicon path"` + } + + R(&EnterpriseUpdateOptions{}, "infos-update", "update enterprise info", func(s *mcclient.ClientSession, args *EnterpriseUpdateOptions) error { + var b bytes.Buffer + w := multipart.NewWriter(&b) + if len(args.Name) > 0 { + w.WriteField("name", args.Name) + } + + if len(args.Copyright) > 0 { + w.WriteField("copyright", args.Copyright) + } + + writeFile := func(filedName, filePath string) error { + if len(filePath) == 0 { + return nil + } + + c, err := ioutil.ReadFile(filePath) + if err != nil { + return err + } + + var v string + c64 := base64.StdEncoding.EncodeToString(c) + ext := filepath.Ext(filePath) + switch ext { + case ".png": + v = fmt.Sprintf("data:image/png;base64,%s", c64) + case ".jpg": + v = fmt.Sprintf("data:image/jpg;base64,%s", c64) + default: + return fmt.Errorf("only support png/jpg image") + } + + err = w.WriteField(filedName, v) + if err != nil { + return err + } + + return nil + } + + if err := writeFile("logo", args.Logo); err != nil { + return err + } + + if err := writeFile("login_logo", args.LoginLogo); err != nil { + return err + } + + if err := writeFile("favicon", args.Favicon); err != nil { + return err + } + + header := make(http.Header) + header.Set("content-type", fmt.Sprintf("multipart/form-data; boundary=%s", w.Boundary())) + w.Close() + + ret, err := modules.Info.Update(s, header, bufio.NewReader(&b)) + if err != nil { + return err + } + + printObject(ret) + return nil + }) }