@@ -10,20 +10,23 @@ import (
1010 "fmt"
1111 "io"
1212 "net/http"
13+ "net/url"
1314 "os"
15+ "path"
1416 "path/filepath"
1517 "runtime"
1618 "sort"
1719 "strings"
1820 "time"
1921
2022 "github.com/aliyun/aliyun-cli/v3/cli"
23+ "github.com/aliyun/aliyun-cli/v3/sysconfig/pluginsettings"
2124 "golang.org/x/mod/semver"
2225)
2326
2427const (
25- IndexURL = "https://aliyun-cli-pub.oss-cn-hangzhou.aliyuncs .com/plugins/plugin_pkg_index.json" // 默认索引地址
26- CommandIndexURL = "https://aliyun-cli-pub.oss-cn-hangzhou.aliyuncs .com/plugins/plugin_search_index.json" // 命令倒排索引地址
28+ IndexURL = "https://aliyuncli.alicdn .com/plugins/plugin_pkg_index.json" // 默认索引地址
29+ CommandIndexURL = "https://aliyuncli.alicdn .com/plugins/plugin_search_index.json" // 命令倒排索引地址
2730 EnvPluginsDir = "ALIBABA_CLOUD_CLI_PLUGINS_DIR"
2831 EnvNoCache = "ALIBABA_CLOUD_CLI_PLUGIN_NO_CACHE"
2932
@@ -43,8 +46,9 @@ func (e *ErrPluginNotFound) Error() string {
4346
4447type Manager struct {
4548 rootDir string
46- indexURL string // For testing: allows overriding IndexURL
47- commandIndexURL string // For testing: allows overriding CommandIndexURL
49+ sourceBase string // from plugin-settings.json / env; empty = use built-in index URLs
50+ indexURL string // For testing: allows overriding resolved package index URL
51+ commandIndexURL string // For testing: allows overriding resolved command index URL
4852}
4953
5054func getHomePath () string {
@@ -86,7 +90,50 @@ func NewManager() (*Manager, error) {
8690 if err := os .MkdirAll (rootDir , 0755 ); err != nil {
8791 return nil , err
8892 }
89- return & Manager {rootDir : rootDir }, nil
93+ sysDir := filepath .Join (getHomePath (), ".aliyun" )
94+ settings , err := pluginsettings .Load (sysDir )
95+ if err != nil {
96+ settings = pluginsettings .Default ()
97+ }
98+ base := pluginsettings .EffectiveSourceBase (settings )
99+ return & Manager {rootDir : rootDir , sourceBase : base }, nil
100+ }
101+
102+ func (m * Manager ) resolvedPkgIndexURL () string {
103+ if m .indexURL != "" {
104+ return m .indexURL
105+ }
106+ if b := strings .TrimRight (strings .TrimSpace (m .sourceBase ), "/" ); b != "" {
107+ return b + "/plugin_pkg_index.json"
108+ }
109+ return IndexURL
110+ }
111+
112+ func (m * Manager ) resolvedCommandIndexURL () string {
113+ if m .commandIndexURL != "" {
114+ return m .commandIndexURL
115+ }
116+ if b := strings .TrimRight (strings .TrimSpace (m .sourceBase ), "/" ); b != "" {
117+ return b + "/plugin_search_index.json"
118+ }
119+ return CommandIndexURL
120+ }
121+
122+ // common layout: .../pkgs/{name}/{version}/{basename}.
123+ func (m * Manager ) resolvePackageDownloadURL (origURL , pluginName , version string ) string {
124+ if strings .TrimSpace (m .sourceBase ) == "" {
125+ return origURL
126+ }
127+ u , err := url .Parse (origURL )
128+ if err != nil {
129+ return origURL
130+ }
131+ baseName := path .Base (u .Path )
132+ if baseName == "" || baseName == "." || baseName == "/" {
133+ return origURL
134+ }
135+ b := strings .TrimRight (strings .TrimSpace (m .sourceBase ), "/" )
136+ return fmt .Sprintf ("%s/pkgs/%s/%s/%s" , b , pluginName , version , baseName )
90137}
91138
92139func (m * Manager ) readCache (cacheFile string , ttl time.Duration , result interface {}) (hit bool , staleAvailable bool ) {
@@ -161,10 +208,7 @@ func (m *Manager) fetchWithCache(url, cacheFile string, result interface{}) erro
161208}
162209
163210func (m * Manager ) GetIndex () (* Index , error ) {
164- indexURL := IndexURL
165- if m .indexURL != "" {
166- indexURL = m .indexURL
167- }
211+ indexURL := m .resolvedPkgIndexURL ()
168212 cacheFile := filepath .Join (m .rootDir , indexCacheFile )
169213 var index Index
170214 if err := m .fetchWithCache (indexURL , cacheFile , & index ); err != nil {
@@ -174,10 +218,7 @@ func (m *Manager) GetIndex() (*Index, error) {
174218}
175219
176220func (m * Manager ) GetCommandIndex () (* CommandIndex , error ) {
177- commandIndexURL := CommandIndexURL
178- if m .commandIndexURL != "" {
179- commandIndexURL = m .commandIndexURL
180- }
221+ commandIndexURL := m .resolvedCommandIndexURL ()
181222 cacheFile := filepath .Join (m .rootDir , commandCacheFile )
182223 var index CommandIndex
183224 if err := m .fetchWithCache (commandIndexURL , cacheFile , & index ); err != nil {
@@ -678,14 +719,18 @@ func (m *Manager) installPlugin(ctx *cli.Context, targetPlugin *PluginInfo, vers
678719 return err
679720 }
680721
681- archivePath , err := m .downloadAndVerifyPlugin (ctx , platInfo , actualPluginName , version )
722+ downloadURL := m .resolvePackageDownloadURL (platInfo .URL , actualPluginName , version )
723+ platForDownload := * platInfo
724+ platForDownload .URL = downloadURL
725+
726+ archivePath , err := m .downloadAndVerifyPlugin (ctx , & platForDownload , actualPluginName , version )
682727 if err != nil {
683728 return err
684729 }
685730 defer os .RemoveAll (filepath .Dir (archivePath ))
686731
687732 extractDir := filepath .Join (m .rootDir , actualPluginName )
688- if err := m .extractPlugin (archivePath , extractDir , platInfo . URL ); err != nil {
733+ if err := m .extractPlugin (archivePath , extractDir , downloadURL ); err != nil {
689734 return err
690735 }
691736
0 commit comments