@@ -33,6 +33,10 @@ import (
3333var (
3434 // ErrInstallFirst indicates that a component/version is not installed
3535 ErrInstallFirst = errors .New ("component not installed" )
36+
37+ // IsPackagedBuild is a flag to indicate if this is a packaged build (e.g., Debian, RPM, Homebrew).
38+ // This should be set to true by the packaging process.
39+ IsPackagedBuild = true // Temporarily set to true for testing packaged build behavior
3640)
3741
3842// Mirror return mirror of tiup.
@@ -81,6 +85,16 @@ func InitEnv(options repository.Options, mOpt repository.MirrorOptions) (*Enviro
8185 return env , nil
8286 }
8387
88+ // Check if running in packaged build mode
89+ if IsPackagedBuild {
90+ fmt .Fprintln (os .Stderr , "Online version check and repository interaction skipped in packaged build." )
91+ // Return a minimal environment that won't try to use the network repo
92+ profile := localdata .InitProfile ()
93+ // We return a valid Environment struct but with v1Repo set to nil.
94+ // Functions that use v1Repo must check for nil or IsPackagedBuild.
95+ return & Environment {profile : profile , v1Repo : nil }, nil
96+ }
97+
8498 initRepo := time .Now ()
8599 profile := localdata .InitProfile ()
86100
@@ -135,6 +149,9 @@ func (env *Environment) LocalPath(path ...string) string {
135149
136150// UpdateComponents updates or installs all components described by specs.
137151func (env * Environment ) UpdateComponents (specs []string , nightly , force bool ) error {
152+ if IsPackagedBuild {
153+ return errors .New ("component updates are disabled in this packaged build" )
154+ }
138155 var v1specs []repository.ComponentSpec
139156 for _ , spec := range specs {
140157 component , v := ParseCompVersion (spec )
@@ -153,6 +170,9 @@ func (env *Environment) PlatformString() string {
153170
154171// SelfUpdate updates TiUP.
155172func (env * Environment ) SelfUpdate () error {
173+ if IsPackagedBuild {
174+ return errors .New ("tiup self-update is disabled in this packaged build" )
175+ }
156176 if err := env .v1Repo .DownloadTiUP (env .LocalPath ("bin" )); err != nil {
157177 return err
158178 }
@@ -162,6 +182,9 @@ func (env *Environment) SelfUpdate() error {
162182}
163183
164184func (env * Environment ) downloadComponentv1 (component string , version utils.Version , overwrite bool ) error {
185+ if IsPackagedBuild {
186+ return errors .Errorf ("downloading component '%s:%s' is disabled in this packaged build" , component , version )
187+ }
165188 spec := repository.ComponentSpec {
166189 ID : component ,
167190 Version : string (version ),
@@ -173,6 +196,9 @@ func (env *Environment) downloadComponentv1(component string, version utils.Vers
173196
174197// downloadComponent downloads the specific version of a component from repository
175198func (env * Environment ) downloadComponent (component string , version utils.Version , overwrite bool ) error {
199+ if IsPackagedBuild {
200+ return errors .Errorf ("downloading component '%s:%s' is disabled in this packaged build" , component , version )
201+ }
176202 return env .downloadComponentv1 (component , version , overwrite )
177203}
178204
@@ -184,6 +210,41 @@ func (env *Environment) SelectInstalledVersion(component string, ver utils.Versi
184210 return ver , err
185211 }
186212
213+ if IsPackagedBuild {
214+ // In packaged builds, we only care about the exact version requested
215+ // or the latest installed version if no version is specified.
216+ // We cannot consult the repository for latest stable or yanked status.
217+ versions := []string {}
218+ for _ , v := range installed {
219+ versions = append (versions , v )
220+ }
221+ // Reverse sort to find the latest installed version
222+ sort .Slice (versions , func (i , j int ) bool {
223+ return semver .Compare (versions [i ], versions [j ]) > 0
224+ })
225+
226+ errInstallFirst := errors .Annotatef (ErrInstallFirst , "component `%s` is not installed locally" , component )
227+ if ! ver .IsEmpty () {
228+ errInstallFirst = errors .Annotatef (ErrInstallFirst , "component `%s:%s` is not installed locally" , component , ver .String ())
229+ }
230+
231+ if ! ver .IsEmpty () {
232+ for _ , v := range versions {
233+ if utils .Version (v ) == ver {
234+ return ver , nil
235+ }
236+ }
237+ return ver , errInstallFirst
238+ } else {
239+ // No version specified, return the latest installed
240+ if len (versions ) > 0 {
241+ return utils .Version (versions [0 ]), nil
242+ }
243+ return ver , errInstallFirst
244+ }
245+ }
246+
247+ // Original logic for non-Debian builds
187248 versions := []string {}
188249 for _ , v := range installed {
189250 vi , err := env .v1Repo .LocalComponentVersion (component , v , true )
@@ -238,6 +299,17 @@ func (env *Environment) SelectInstalledVersion(component string, ver utils.Versi
238299
239300// DownloadComponentIfMissing downloads the specific version of a component if it is missing
240301func (env * Environment ) DownloadComponentIfMissing (component string , ver utils.Version ) (utils.Version , error ) {
302+ if IsPackagedBuild {
303+ // Downloading components is disabled in packaged builds.
304+ // We should only rely on components installed via the package.
305+ // Check if the requested version is installed locally.
306+ selectedVer , err := env .SelectInstalledVersion (component , ver )
307+ if err != nil {
308+ return "" , errors .Annotatef (err , "component '%s:%s' is not installed locally in this packaged build" , component , ver .String ())
309+ }
310+ return selectedVer , nil
311+ }
312+
241313 var err error
242314 if ver .String () == utils .NightlyVersionAlias {
243315 if ver , _ , err = env .v1Repo .LatestNightlyVersion (component ); err != nil {
@@ -282,6 +354,15 @@ func (env *Environment) BinaryPath(component string, ver utils.Version) (string,
282354 return "" , err
283355 }
284356
357+ if IsPackagedBuild {
358+ // In packaged builds, the binary path is simply the install path
359+ // followed by the component name. We don't use the repository object
360+ // to determine the binary name within the installation directory.
361+ // Assuming the binary name is the same as the component name.
362+ // This might need adjustment if binary names differ from component names.
363+ return filepath .Join (installPath , component ), nil
364+ }
365+
285366 return env .v1Repo .BinaryPath (installPath , component , ver .String ())
286367}
287368
0 commit comments