Skip to content

Commit

Permalink
Complete warship upload file function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Meaglith Ma committed Jun 22, 2017
1 parent 67ee731 commit 1408bb3
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 2 deletions.
68 changes: 68 additions & 0 deletions dockyard/client/binary/binary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2016 - 2017 Huawei Technologies Co., Ltd. All rights reserved.
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
http://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 binary

import (
"fmt"
"net/http"
"os"
"path/filepath"
)

// Upload binary file to the Dockyard service.
func UploadBinaryFile(filePath, domain, namespace, repository, tag string) error {
if f, err := os.Open(filePath); err != nil {
return err
} else {
defer f.Close()

fmt.Println(fmt.Sprintf("https://%s/binary/v1/%s/%s/binary/%s/%s",
domain, namespace, repository, filepath.Base(filePath), tag))

if req, err := http.NewRequest(http.MethodPut,
fmt.Sprintf("https://%s/binary/v1/%s/%s/binary/%s/%s",
domain, namespace, repository, filepath.Base(filePath), tag), f); err != nil {
return err
} else {
req.Header.Set("Content-Type", "text/plain")

client := &http.Client{}
if resp, err := client.Do(req); err != nil {
return err
} else {
defer resp.Body.Close()

switch resp.StatusCode {
case http.StatusOK:
return nil
case http.StatusBadRequest:
return fmt.Errorf("Binary upload failed.")
case http.StatusUnauthorized:
return fmt.Errorf("Action unauthorized.")
default:
return fmt.Errorf("Unknown error.")
}
}
}
}

return nil
}

func DownloadBinaryFile(domain, namespace, repository, filename, tag, filePath string) error {
return nil
}
85 changes: 83 additions & 2 deletions dockyard/client/cmd/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,92 @@ limitations under the License.
package cmd

import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"

"github.com/Huawei/containerops/common"
"github.com/Huawei/containerops/dockyard/client/binary"
)

var binaryCmd = &cobra.Command{
Use: "binary",
Short: "binary to repository",
Long: ``,
Short: "Upload or download file with Dockyard service",
Long: `Use binary sub command to upload or download binary file from Dockyard service.
Upload file to a repository of Dockyard:
warship binary upload --domain hub.opshub.sh /tmp/warship containerops/cncf-demo/stichers
The upload URI pattern is <namespace>/<repository>/<tag>
Download file from repository of Dockyard:
warship binary download --domain hub.opshub.sh containerops/cncf-demo/warship/strichers
The download URI pattern is <namespace>/<repository>/<filename>/<tag>
`,
}

var uplaodCmd = &cobra.Command{
Use: "upload",
Short: "Upload file to Dockyard service, `warship binary upload --domain hub.opshub.sh <filename> <namespace>/<repository>/<tag>`",
Long: `Upload file to a repository of Dockyard:
warship binary upload --domain hub.opshub.sh /tmp/warship hub.opshub.sh/containerops/cncf-demo/stichers
The upload URI pattern is <namespace>/<repository>/<tag>`,
Run: uploadBinary,
}

var downloadCmd = &cobra.Command{
Use: "download",
Short: "Download file form Dockyard service, `warship binary download <namespace>/<repository>/<filename>/<tag>",
Long: `Download file from repository of Dockyard:
warship binary download --domain hub.opshub.sh containerops/cncf-demo/warship/strichers
The download URI pattern is <namespace>/<repository>/<filename>/<tag>`,
Run: downloadBinary,
}

// init()
func init() {
RootCmd.AddCommand(binaryCmd)

//Add create repository sub command.
binaryCmd.AddCommand(uplaodCmd)
binaryCmd.AddCommand(downloadCmd)

}

// Upload binary to Dockyard service.
func uploadBinary(cmd *cobra.Command, args []string) {
if domain == "" {
domain = common.Warship.Domain
}

if len(args) <= 0 {
fmt.Println("The file path and upload uri is required.")
os.Exit(1)
}
namespace := strings.Split(args[1], "/")[0]
repository := strings.Split(args[1], "/")[1]
tag := strings.Split(args[1], "/")[2]

if err := binary.UploadBinaryFile(args[0], domain, namespace, repository, tag); err != nil {
fmt.Println("Upload file error: ", err.Error())
os.Exit(1)
}

fmt.Println("Upload file sucessfully.")
os.Exit(0)
}

func downloadBinary(cmd *cobra.Command, args []string) {
if domain == "" {
domain = common.Warship.Domain
}
}

0 comments on commit 1408bb3

Please sign in to comment.