Skip to content

Commit

Permalink
Merge pull request #37 from Doer-org/feature/hostname-option
Browse files Browse the repository at this point in the history
feat(push) : Add host option
  • Loading branch information
seipan authored May 10, 2024
2 parents b9beafe + f150d4d commit 9177fbb
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
7 changes: 6 additions & 1 deletion cmd/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ var pullCmd = &cobra.Command{
if err != nil {
return err
}
host, err := cmd.Flags().GetString("host")
if err != nil {
return err
}

port, err := api.ReceiveTarGzFromServer(id)
port, err := api.ReceiveTarGzFromServer(host, id)
if err != nil {
return err
}
Expand All @@ -48,6 +52,7 @@ var pullCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(pullCmd)
pullCmd.Flags().StringP("id", "i", "", "ketos docker image id")
pushCmd.Flags().StringP("host", "h", "", "URL of the server to use")

// Here you will define your flags and configuration settings.

Expand Down
7 changes: 6 additions & 1 deletion cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ var pushCmd = &cobra.Command{
if err != nil {
return err
}
host, err := cmd.Flags().GetString("host")
if err != nil {
return err
}
dockerfile, err := cmd.Flags().GetBool("dockerfile")
if err != nil {
return err
Expand All @@ -57,7 +61,7 @@ var pushCmd = &cobra.Command{
if err != nil {
return err
}
err = api.SendTarToServer(publishList, envList)
err = api.SendTarToServer(host, publishList, envList)
if err != nil {
return err
}
Expand All @@ -70,6 +74,7 @@ func init() {
pushCmd.Flags().StringP("directory", "d", "", "Directory path to create docker image")
pushCmd.Flags().StringP("language", "l", "", "Language type to create docker image")
pushCmd.Flags().StringP("filename", "f", "", "Dockerfile name to create docker image")
pushCmd.Flags().StringP("host", "h", "", "URL of the server to use")
pushCmd.Flags().BoolP("dockerfile", "D", false, "Dockerfile or buildpacks")
pushCmd.Flags().StringSliceP("publish", "p", []string{}, "Publish a container's port(s) to the host")
pushCmd.Flags().StringSliceP("env", "e", []string{}, "Set environment variable(s)")
Expand Down
5 changes: 0 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

Expand All @@ -10,8 +9,6 @@ import (
"github.com/spf13/cobra"
)



// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "ketos",
Expand Down Expand Up @@ -47,5 +44,3 @@ func init() {
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}


17 changes: 10 additions & 7 deletions internal/api/receive_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
)

type Response struct {
Port string `json:"port"`
ID string `json:"id"`
Port string `json:"port"`
ID string `json:"id"`
}

func GetServerInfo(id string) (string,error) {
func GetServerInfo(id string) (string, error) {
fullURL := fmt.Sprintf("%s/info/%s", BackendURL, id)
response, err := http.Get(fullURL)
if err != nil {
Expand All @@ -33,19 +33,22 @@ func GetServerInfo(id string) (string,error) {
if err != nil {
return "", err
}
fmt.Printf("%s/info/%s\n", FrontURL, id)
fmt.Printf("%s/info/%s\n", FrontURL, id)
return resp.Port, nil
}

func ReceiveTarGzFromServer(id string) (string, error) {
fullURL := fmt.Sprintf("%s/%s", BackendURL, id)
func ReceiveTarGzFromServer(host string, id string) (string, error) {
if host == "" {
host = BackendURL
}
fullURL := fmt.Sprintf("%s/%s", host, id)
response, err := http.Get(fullURL)
if err != nil {
return "", err
}
defer response.Body.Close()

infoURL := fmt.Sprintf("%s/info/%s", BackendURL, id)
infoURL := fmt.Sprintf("%s/info/%s", host, id)
infoResponse, err := http.Get(infoURL)
if err != nil {
return "", err
Expand Down
10 changes: 7 additions & 3 deletions internal/api/send_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import (
"github.com/Doer-org/ketos/internal"
)

func SendTarToServer(publishList []string, envList []string) error {
func SendTarToServer(host string, publishList []string, envList []string) error {
if host == "" {
host = BackendURL
}

file, err := os.Open(filePath)
if err != nil {
return err
Expand All @@ -38,7 +42,7 @@ func SendTarToServer(publishList []string, envList []string) error {
portsString = "none"
}

fullURL := fmt.Sprintf("%s?port=%s", BackendURL, portsString)
fullURL := fmt.Sprintf("%s?port=%s", host, portsString)
request, err := http.NewRequest("POST", fullURL, body)
if err != nil {
return err
Expand All @@ -51,7 +55,7 @@ func SendTarToServer(publishList []string, envList []string) error {
return err
}
defer response.Body.Close()

var responseBody bytes.Buffer
_, err = io.Copy(&responseBody, response.Body)
if err != nil {
Expand Down

0 comments on commit 9177fbb

Please sign in to comment.