From 2ef3e91e0904847aa2a4c49a63c65bd71a814a3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9E=D0=BB=D0=B5=D0=B3=20=D0=91=D0=BE=D1=80=D0=BE=D0=B4?= =?UTF-8?q?=D0=B8=D0=BD?= Date: Thu, 29 Jan 2026 18:02:12 +0200 Subject: [PATCH] working commit --- cmd/mstorectl/main.go | 10 ++--- pkg/client/client.go | 102 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 103 insertions(+), 9 deletions(-) diff --git a/cmd/mstorectl/main.go b/cmd/mstorectl/main.go index bab6457..94d3c80 100644 --- a/cmd/mstorectl/main.go +++ b/cmd/mstorectl/main.go @@ -3,7 +3,7 @@ package main import ( "fmt" "os" - "path/filepath" + "path/filepath" "github.com/spf13/cobra" "sigs.k8s.io/yaml" @@ -34,11 +34,11 @@ func NewUtil() *Util { func (util *Util) Build() error { var err error - execName := filepath.Base(os.Args[0]) + execName := filepath.Base(os.Args[0]) rootCmd := cobra.Command{ - Use: execName, - Short: "\nA brief description the command", - SilenceUsage: true, + Use: execName, + Short: "\nA brief description the command", + SilenceUsage: true, } rootCmd.CompletionOptions.DisableDefaultCmd = true rootCmd.AddCommand(util.CreateFileCmds()) diff --git a/pkg/client/client.go b/pkg/client/client.go index a5e0a10..a221231 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -1,9 +1,103 @@ package client -type Client struct { +import ( + "context" + "crypto/tls" + "fmt" + "io" + "net/http" + "net/url" + "os" + "path" + "path/filepath" + "strings" +) + +type Client struct{} + +func NewClient() *Client { + return &Client{} } -func (cli *Client) FileExists(path string) (bool, error) { - //reqPath = fmt.Sprintf("/v3/api/file/%s", path) - //request, err := http.NewRequest("HEAD", reqPath, nil) +func (cli *Client) FileExists(ctx context.Context, reference string) (bool, error) { + var res bool + var err error + + reqpath := fmt.Sprintf("/v3/api/file/%s", reference) + + req, err := http.NewRequestWithContext(ctx, "HEAD", reqpath, nil) + if err != nil { + return res, err + } + transport := &http.Transport{ + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: true, + }, + } + client := &http.Client{ + Transport: transport, + } + resp, err := client.Do(req) + if err != nil { + return res, err + } + defer resp.Body.Close() + if resp.StatusCode == http.StatusOK { + res = true + } + return res, err +} + +func (cli *Client) GetFile(ctx context.Context, ref, filename string) error { + var err error + + const api = "/v3/api/file/" + + if !strings.Contains(ref, "://") { + ref = "https://" + ref + } + url, err := url.Parse(ref) + if err != nil { + return err + } + url.Path = path.Join(api, url.Path) + url.User = nil + ref = url.String() + + req, err := http.NewRequestWithContext(ctx, "GET", ref, nil) + if err != nil { + return err + } + transport := &http.Transport{ + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: true, + }, + } + client := &http.Client{ + Transport: transport, + } + resp, err := client.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + err := fmt.Errorf("Received wrong status code: %s", resp.StatusCode) + return err + } + + dirname := filepath.Dir(filename) + err = os.MkdirAll(dirname, 0750) + if err != nil { + return err + } + file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0640) + if err != nil { + return err + } + _, err = io.Copy(file, resp.Body) + if err != nil { + return err + } + return err }