From b6ea0e0f59ee34307c8a1e2448a2840cf295856a Mon Sep 17 00:00:00 2001 From: wheelchairy Date: Tue, 4 Feb 2025 18:30:33 +0300 Subject: [PATCH] upd2 --- cmd/info.go | 3 +- cmd/install.go | 3 +- cmd/list.go | 3 +- cmd/remove.go | 3 +- cmd/repo.go | 3 +- cmd/root.go | 7 ++-- cmd/update.go | 3 +- config.json | 5 ++- go.mod | 2 +- pkg/autoupdate/autoupdate.go | 58 +++++++++++++++++++++++++++++++ pkg/cache/cache.go | 50 ++++++++++++++++++++++++++ pkg/config/config.go | 11 +++--- pkg/dependencies/dependencies.go | 16 +++++++++ pkg/manifest/manifest.go | 56 ++++++++++++++++------------- pkg/plugin/plugin.go | 15 ++++++++ pkg/security/.DS_Store | Bin 0 -> 6148 bytes pkg/security/security.go | 14 ++++++++ pkg/version/version.go | 42 ++++++++++++++++++++++ 18 files changed, 245 insertions(+), 49 deletions(-) create mode 100644 pkg/autoupdate/autoupdate.go create mode 100644 pkg/cache/cache.go create mode 100644 pkg/dependencies/dependencies.go create mode 100644 pkg/plugin/plugin.go create mode 100644 pkg/security/.DS_Store create mode 100644 pkg/security/security.go create mode 100644 pkg/version/version.go diff --git a/cmd/info.go b/cmd/info.go index d5d9ffc..901f58f 100644 --- a/cmd/info.go +++ b/cmd/info.go @@ -3,7 +3,6 @@ package cmd import ( "fmt" "train/pkg/installer" - "github.com/spf13/cobra" ) @@ -20,4 +19,4 @@ var infoCmd = &cobra.Command{ } fmt.Printf("Информация о пакете %s:\n%s\n", pkgName, info) }, -} +} \ No newline at end of file diff --git a/cmd/install.go b/cmd/install.go index df2a309..919c010 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -4,7 +4,6 @@ import ( "fmt" "os" "train/pkg/installer" - "github.com/spf13/cobra" ) @@ -21,4 +20,4 @@ var installCmd = &cobra.Command{ } fmt.Printf("Пакет %s успешно установлен.\n", pkgName) }, -} +} \ No newline at end of file diff --git a/cmd/list.go b/cmd/list.go index c064581..d19c0f7 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -3,7 +3,6 @@ package cmd import ( "fmt" "train/pkg/installer" - "github.com/spf13/cobra" ) @@ -21,4 +20,4 @@ var listCmd = &cobra.Command{ fmt.Println(" -", p) } }, -} +} \ No newline at end of file diff --git a/cmd/remove.go b/cmd/remove.go index 789876a..1a37708 100644 --- a/cmd/remove.go +++ b/cmd/remove.go @@ -4,7 +4,6 @@ import ( "fmt" "os" "train/pkg/installer" - "github.com/spf13/cobra" ) @@ -21,4 +20,4 @@ var removeCmd = &cobra.Command{ } fmt.Printf("Пакет %s успешно удалён.\n", pkgName) }, -} +} \ No newline at end of file diff --git a/cmd/repo.go b/cmd/repo.go index 10ece52..d3893c4 100644 --- a/cmd/repo.go +++ b/cmd/repo.go @@ -4,7 +4,6 @@ import ( "fmt" "os" "train/pkg/config" - "github.com/spf13/cobra" ) @@ -61,4 +60,4 @@ func init() { repoCmd.AddCommand(repoAddCmd) repoCmd.AddCommand(repoRemoveCmd) repoCmd.AddCommand(repoListCmd) -} +} \ No newline at end of file diff --git a/cmd/root.go b/cmd/root.go index 6a19665..31abb85 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,9 +1,8 @@ package cmd import ( - "fmt" "os" - + "fmt" "github.com/spf13/cobra" ) @@ -25,5 +24,5 @@ func init() { rootCmd.AddCommand(updateCmd) rootCmd.AddCommand(listCmd) rootCmd.AddCommand(infoCmd) - rootCmd.AddCommand(repoCmd) -} + rootCmd.AddCommand(repoCmd) +} \ No newline at end of file diff --git a/cmd/update.go b/cmd/update.go index e11d5e9..52819e7 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -4,7 +4,6 @@ import ( "fmt" "os" "train/pkg/installer" - "github.com/spf13/cobra" ) @@ -21,4 +20,4 @@ var updateCmd = &cobra.Command{ } fmt.Printf("Пакет %s успешно обновлён.\n", pkgName) }, -} +} \ No newline at end of file diff --git a/config.json b/config.json index 5ca4e6e..55899fe 100644 --- a/config.json +++ b/config.json @@ -1,6 +1,5 @@ { "repositories": [ - "http://xn--80abmlgju2eo3byb.xn--p1ai/sklad/" - ], - } + "https://xn--80abmlgju2eo3byb.xn--p1ai/sklad" + ] } diff --git a/go.mod b/go.mod index 2e60e31..d3bf8b4 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module train go 1.18 -require github.com/spf13/cobra v1.7.0 // или актуальная версия +require github.com/spf13/cobra v1.7.0 require ( github.com/inconshreveable/mousetrap v1.1.0 // indirect diff --git a/pkg/autoupdate/autoupdate.go b/pkg/autoupdate/autoupdate.go new file mode 100644 index 0000000..2dbdaed --- /dev/null +++ b/pkg/autoupdate/autoupdate.go @@ -0,0 +1,58 @@ +package autoupdate + +import ( + "fmt" + "io/ioutil" + "net/http" + "os" + "os/exec" +) + +func CheckForUpdates(updateURL string) (bool, string, error) { + resp, err := http.Get(updateURL) + if err != nil { + return false, "", err + } + defer resp.Body.Close() + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + return false, "", err + } + latestVersion := string(data) + currentVersion := "1.0.0" + if latestVersion != currentVersion { + return true, latestVersion, nil + } + return false, currentVersion, nil +} + +func AutoUpdate(updateURL string) error { + available, latest, err := CheckForUpdates(updateURL) + if err != nil { + return err + } + if available { + fmt.Printf("Доступна новая версия: %s. Запускаем обновление...\n", latest) + resp, err := http.Get(updateURL + "/binary") // предположим, что по этому URL лежит бинарник + if err != nil { + return err + } + defer resp.Body.Close() + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + tmpFile := "/tmp/train_new" + if err := ioutil.WriteFile(tmpFile, data, 0755); err != nil { + return err + } + cmd := exec.Command(tmpFile) + if err := cmd.Start(); err != nil { + return err + } + os.Exit(0) + } else { + fmt.Println("Обновлений не найдено.") + } + return nil +} \ No newline at end of file diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go new file mode 100644 index 0000000..8d08b65 --- /dev/null +++ b/pkg/cache/cache.go @@ -0,0 +1,50 @@ +package cache + +import ( + "fmt" + "io/ioutil" + "net/http" + "os" + "path/filepath" +) + +var cacheDir = "./cache" + +func GetCachedFile(url string) (string, error) { + if err := os.MkdirAll(cacheDir, os.ModePerm); err != nil { + return "", err + } + fileName := filepath.Join(cacheDir, filepath.Base(url)) + if _, err := os.Stat(fileName); err == nil { + return fileName, nil + } + return "", nil +} + +func SaveToCache(url string, data []byte) (string, error) { + if err := os.MkdirAll(cacheDir, os.ModePerm); err != nil { + return "", err + } + fileName := filepath.Join(cacheDir, filepath.Base(url)) + if err := ioutil.WriteFile(fileName, data, 0644); err != nil { + return "", err + } + fmt.Printf("Сохранено в кэш: %s\n", fileName) + return fileName, nil +} + +func DownloadWithCache(url string) (string, error) { + if cached, err := GetCachedFile(url); err == nil && cached != "" { + return cached, nil + } + resp, err := http.Get(url) + if err != nil { + return "", err + } + defer resp.Body.Close() + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + return "", err + } + return SaveToCache(url, data) +} \ No newline at end of file diff --git a/pkg/config/config.go b/pkg/config/config.go index 785021f..fb13e50 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -8,20 +8,23 @@ import ( ) type Config struct { - Repositories []string `json:"repositories"` + Repositories []string `json:"repositories"` + Packages map[string]string `json:"packages"` } var configFile = "./config.json" func LoadConfig() (*Config, error) { if _, err := os.Stat(configFile); os.IsNotExist(err) { - cfg := &Config{Repositories: []string{}} + cfg := &Config{ + Repositories: []string{}, + Packages: make(map[string]string), + } if err := SaveConfig(cfg); err != nil { return nil, err } return cfg, nil } - data, err := ioutil.ReadFile(configFile) if err != nil { return nil, err @@ -74,4 +77,4 @@ func RemoveRepository(repo string) error { } cfg.Repositories = newRepos return SaveConfig(cfg) -} +} \ No newline at end of file diff --git a/pkg/dependencies/dependencies.go b/pkg/dependencies/dependencies.go new file mode 100644 index 0000000..bd7fba0 --- /dev/null +++ b/pkg/dependencies/dependencies.go @@ -0,0 +1,16 @@ +package dependencies + +import ( + "fmt" + "train/pkg/installer" +) + +func InstallDependencies(deps []string) error { + for _, dep := range deps { + fmt.Printf("Устанавливаем зависимость: %s\n", dep) + if err := installer.InstallPackage(dep); err != nil { + return fmt.Errorf("не удалось установить зависимость %s: %v", dep, err) + } + } + return nil +} \ No newline at end of file diff --git a/pkg/manifest/manifest.go b/pkg/manifest/manifest.go index cf76198..107d10f 100644 --- a/pkg/manifest/manifest.go +++ b/pkg/manifest/manifest.go @@ -12,13 +12,12 @@ import ( type Manifest struct { Name string `json:"name"` Version string `json:"version"` - Mode string `json:"mode"` - Source string `json:"source"` + Mode string `json:"mode"` + Source string `json:"source"` Checksum string `json:"checksum"` Dependencies []string `json:"dependencies"` - Build []string `json:"build"` - Binaries map[string]string `json:"binaries"` - runtime.GOOS+"_"+runtime.GOARCH + Build []string `json:"build"` + Binaries map[string]string `json:"binaries"` } func FetchManifest(name string) (*Manifest, error) { @@ -26,33 +25,40 @@ func FetchManifest(name string) (*Manifest, error) { if err != nil { return nil, err } + if url, ok := cfg.Packages[name]; ok { + return fetchManifestFromURL(url) + } var lastErr error for _, repo := range cfg.Repositories { url := fmt.Sprintf("%s/%s/manifest.json", repo, name) - resp, err := http.Get(url) - if err != nil { - lastErr = err - continue + m, err := fetchManifestFromURL(url) + if err == nil { + return m, nil } - defer resp.Body.Close() - if resp.StatusCode != 200 { - lastErr = errors.New("пакет не найден в репозитории " + repo) - continue - } - data, err := ioutil.ReadAll(resp.Body) - if err != nil { - lastErr = err - continue - } - var m Manifest - if err := json.Unmarshal(data, &m); err != nil { - lastErr = err - continue - } - return &m, nil + lastErr = err } if lastErr == nil { lastErr = errors.New("не найден ни один репозиторий") } return nil, lastErr } + +func fetchManifestFromURL(url string) (*Manifest, error) { + resp, err := http.Get(url) + if err != nil { + return nil, err + } + defer resp.Body.Close() + if resp.StatusCode != 200 { + return nil, errors.New("манифест пакета не найден по ссылке " + url) + } + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + var m Manifest + if err := json.Unmarshal(data, &m); err != nil { + return nil, err + } + return &m, nil +} \ No newline at end of file diff --git a/pkg/plugin/plugin.go b/pkg/plugin/plugin.go new file mode 100644 index 0000000..4befead --- /dev/null +++ b/pkg/plugin/plugin.go @@ -0,0 +1,15 @@ +package plugin + +import ( + "fmt" + "plugin" +) + +func LoadPlugin(path string) (*plugin.Plugin, error) { + p, err := plugin.Open(path) + if err != nil { + return nil, err + } + fmt.Printf("Плагин загружен: %s\n", path) + return p, nil +} \ No newline at end of file diff --git a/pkg/security/.DS_Store b/pkg/security/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0