go.fuchsia.dev/jiri@v0.0.0-20240502161911-b66513b29486/cmd/jiri/resolve.go (about)

     1  // Copyright 2018 The Fuchsia Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"strings"
     9  
    10  	"go.fuchsia.dev/jiri"
    11  	"go.fuchsia.dev/jiri/cmdline"
    12  	"go.fuchsia.dev/jiri/project"
    13  )
    14  
    15  type resolveFlags struct {
    16  	lockFilePath         string
    17  	localManifestFlag    bool
    18  	enablePackageLock    bool
    19  	enableProjectLock    bool
    20  	enablePackageVersion bool
    21  	allowFloatingRefs    bool
    22  	fullResolve          bool
    23  	hostnameAllowList    string
    24  }
    25  
    26  func (r *resolveFlags) AllowFloatingRefs() bool {
    27  	return r.allowFloatingRefs
    28  }
    29  
    30  func (r *resolveFlags) LockFilePath() string {
    31  	return r.lockFilePath
    32  }
    33  
    34  func (r *resolveFlags) LocalManifest() bool {
    35  	return r.localManifestFlag
    36  }
    37  
    38  func (r *resolveFlags) EnablePackageLock() bool {
    39  	return r.enablePackageLock
    40  }
    41  
    42  func (r *resolveFlags) EnableProjectLock() bool {
    43  	return r.enableProjectLock
    44  }
    45  
    46  func (r *resolveFlags) HostnameAllowList() []string {
    47  	ret := make([]string, 0)
    48  	hosts := strings.Split(r.hostnameAllowList, ",")
    49  	for _, item := range hosts {
    50  		item = strings.TrimSpace(item)
    51  		if item == "" {
    52  			continue
    53  		}
    54  		ret = append(ret, item)
    55  	}
    56  	return ret
    57  }
    58  
    59  func (r *resolveFlags) FullResolve() bool {
    60  	return r.fullResolve
    61  }
    62  
    63  var resolveFlag resolveFlags
    64  
    65  var cmdResolve = &cmdline.Command{
    66  	Runner: jiri.RunnerFunc(runResolve),
    67  	Name:   "resolve",
    68  	Short:  "Generate jiri lockfile",
    69  	Long: `
    70  Generate jiri lockfile in json format for <manifest ...>. If no manifest
    71  provided, jiri will use .jiri_manifest by default.
    72  `,
    73  	ArgsName: "<manifest ...>",
    74  	ArgsLong: "<manifest ...> is a list of manifest files for lockfile generation",
    75  }
    76  
    77  func init() {
    78  	flags := &cmdResolve.Flags
    79  	flags.StringVar(&resolveFlag.lockFilePath, "output", "jiri.lock", "Path to the generated lockfile")
    80  	flags.BoolVar(&resolveFlag.localManifestFlag, "local-manifest", false, "Use local manifest")
    81  	flags.BoolVar(&resolveFlag.enablePackageLock, "enable-package-lock", true, "Enable resolving packages in lockfile")
    82  	flags.BoolVar(&resolveFlag.enableProjectLock, "enable-project-lock", false, "Enable resolving projects in lockfile")
    83  	flags.BoolVar(&resolveFlag.allowFloatingRefs, "allow-floating-refs", false, "Allow packages to be pinned to floating refs such as \"latest\"")
    84  	flags.StringVar(&resolveFlag.hostnameAllowList, "allow-hosts", "", "List of hostnames that can be used in the url of a repository, seperated by comma. It will not be enforced if it is left empty.")
    85  	flags.BoolVar(&resolveFlag.fullResolve, "full-resolve", false, "Resolve all project and packages, not just those are changed.")
    86  }
    87  
    88  func runResolve(jirix *jiri.X, args []string) error {
    89  	manifestFiles := make([]string, 0)
    90  	if len(args) == 0 {
    91  		// Use .jiri_manifest if no manifest file path is present
    92  		manifestFiles = append(manifestFiles, jirix.JiriManifestFile())
    93  	} else {
    94  		for _, m := range args {
    95  			manifestFiles = append(manifestFiles, m)
    96  		}
    97  	}
    98  	// While revision pins for projects can be updated by 'jiri edit',
    99  	// instance IDs of packages can only be updated by 'jiri resolve' due
   100  	// to the way how cipd works. Since roller is using 'jiri resolve'
   101  	// to update a single jiri.lock file each time, it will cause conflicting
   102  	// instance ids between updated 'jiri.lock' and un-updated 'jiri.lock' files.
   103  	// Jiri will halt when detecting conflicts in locks. So to make it work,
   104  	// we need to temporarily disable the conflicts detection.
   105  	jirix.IgnoreLockConflicts = true
   106  	return project.GenerateJiriLockFile(jirix, manifestFiles, &resolveFlag)
   107  }