func runList(env *cmdline.Env, args []string) error { ctx := tool.NewContextFromEnv(env) if showManifestFlag { data, err := ctx.Run().ReadFile(manifestFlag) if err != nil { return err } fmt.Fprintln(ctx.Stdout(), string(data)) return nil } if verboseFlag { fmt.Fprintf(ctx.Stdout(), "Manifest: %s\n", manifestFlag) } if availableFlag { if verboseFlag { fmt.Fprintf(ctx.Stdout(), "Available Profiles:\n") for _, name := range profiles.Managers() { mgr := profiles.LookupManager(name) vi := mgr.VersionInfo() fmt.Fprintf(ctx.Stdout(), "%s: versions: %s - %s\n", name, vi.Default(), strings.Join(vi.Supported(), " ")) } } else { fmt.Fprintf(ctx.Stdout(), "%s\n", strings.Join(profiles.Managers(), ", ")) } } if err := profiles.Read(ctx, manifestFlag); err != nil { fmt.Fprintf(ctx.Stderr(), "Failed to read manifest: %v", err) return err } profileNames := args if len(args) == 0 { profileNames = profiles.Profiles() } availableNames := []string{} for _, name := range profileNames { if profiles.LookupProfile(name) != nil { availableNames = append(availableNames, name) } } if verboseFlag { fmt.Fprintf(ctx.Stdout(), "Installed Profiles: ") fmt.Fprintf(ctx.Stdout(), "%s\n", strings.Join(profiles.Profiles(), ", ")) for _, name := range availableNames { profile := profiles.LookupProfile(name) fmt.Fprintf(ctx.Stdout(), "Profile: %s @ %s\n", profile.Name, profile.Root) for _, target := range profile.Targets() { fmt.Fprintf(ctx.Stdout(), "\t%s\n", target.DebugString()) } } } else { for _, name := range availableNames { profile := profiles.LookupProfile(name) for _, target := range profile.Targets() { fmt.Fprintf(ctx.Stdout(), "%s %s\n", name, target) } } } return nil }
func runInstall(env *cmdline.Env, args []string) error { ctx := tool.NewContextFromEnv(env) if err := initCommand(ctx, args); err != nil { return err } names := []string{} if len(args) == 0 { for _, name := range profiles.Managers() { names = append(names, name) } } for _, name := range args { if p := profiles.LookupProfileTarget(name, targetFlag); p != nil { fmt.Fprintf(ctx.Stdout(), "%v %v is already installed as %v\n", name, targetFlag, p) continue } names = append(names, name) } if err := applyCommand(names, env, ctx, targetFlag, func(mgr profiles.Manager, ctx *tool.Context, target profiles.Target) error { err := mgr.Install(ctx, target) logResult(ctx, "Install:", mgr, target, err) return err }); err != nil { return err } return profiles.Write(ctx, manifestFlag) }
func Init(defaultManifestFilename string) { targetFlag = profiles.DefaultTarget() var err error rootDir, err = project.JiriRoot() if err != nil { panic(err) } manifest := filepath.Join(rootDir, defaultManifestFilename) // Every sub-command accepts: --manifest for _, fs := range []*flag.FlagSet{ &cmdInstall.Flags, &cmdUpdate.Flags, &cmdCleanup.Flags, &cmdUninstall.Flags, &cmdEnv.Flags, &cmdList.Flags, &cmdRecreate.Flags} { profiles.RegisterManifestFlag(fs, &manifestFlag, manifest) } // install accepts: --target and, --env. profiles.RegisterTargetAndEnvFlags(&cmdInstall.Flags, &targetFlag) // uninstall and env accept: --target, for _, fs := range []*flag.FlagSet{ &cmdUninstall.Flags, &cmdEnv.Flags} { profiles.RegisterTargetFlag(fs, &targetFlag) } // uninstall accept --all-targets but with different defaults. cmdUninstall.Flags.BoolVar(&allFlag, "all-targets", false, "apply to all targets for the specified profile(s)") // update accepts --v cmdUpdate.Flags.BoolVar(&verboseFlag, "v", false, "print more detailed information") // list accepts --show-manifest, --availabe, --v cmdList.Flags.BoolVar(&showManifestFlag, "show-manifest", false, "print out the manifest file") cmdList.Flags.BoolVar(&availableFlag, "available", false, "print the list of available profiles") cmdList.Flags.BoolVar(&verboseFlag, "v", false, "print more detailed information") // env accepts --profile cmdEnv.Flags.StringVar(&profileFlag, "profile", "", "the profile whose environment is to be displayed") for _, mgr := range profiles.Managers() { profiles.LookupManager(mgr).AddFlags(&cmdInstall.Flags, profiles.Install) profiles.LookupManager(mgr).AddFlags(&cmdUninstall.Flags, profiles.Uninstall) } }
func runInfo(env *cmdline.Env, args []string) error { ctx := tool.NewContextFromEnv(env) profileNames := args if len(args) == 0 { profileNames = profiles.Managers() } _, width, err := textutil.TerminalSize() if err != nil { width = 80 } w := textutil.NewUTF8LineWriter(ctx.Stdout(), width) defer w.Flush() for _, name := range profileNames { mgr := profiles.LookupManager(name) if mgr == nil { return fmt.Errorf("profile %q is not available", name) } fmt.Fprintf(w, "%s: %s\n\n", name, mgr.Info()) } return nil }
func runCleanup(env *cmdline.Env, args []string) error { ctx := tool.NewContextFromEnv(env) if len(args) == 0 { args = profiles.Managers() } if err := initCommand(ctx, args); err != nil { return err } for _, n := range args { mgr := profiles.LookupManager(n) vi := mgr.VersionInfo() profile := profiles.LookupProfile(n) for _, target := range profile.Targets() { if vi.IsOlderThanDefault(target.Version()) { err := mgr.Uninstall(ctx, *target) logResult(ctx, "Cleanup", mgr, *target, err) if err != nil { return err } } } } return profiles.Write(ctx, manifestFlag) }
func runUpdate(env *cmdline.Env, args []string) error { ctx := tool.NewContextFromEnv(env) if len(args) == 0 { args = profiles.Managers() } if err := initCommand(ctx, args); err != nil { return err } for _, n := range args { mgr := profiles.LookupManager(n) profile := profiles.LookupProfile(n) if profile == nil { continue } vi := mgr.VersionInfo() mgr.SetRoot(rootDir) for _, target := range profile.Targets() { if vi.IsNewerThanDefault(target.Version()) { if verboseFlag { fmt.Fprintf(ctx.Stdout(), "Updating %s %s from %q to %s\n", n, target, target.Version(), vi) } target.SetVersion(vi.Default()) err := mgr.Install(ctx, *target) logResult(ctx, "Update", mgr, *target, err) if err != nil { return err } } else { if verboseFlag { fmt.Fprintf(ctx.Stdout(), "%s %s at %q is up to date(%s)\n", n, target, target.Version(), vi) } } } } return profiles.Write(ctx, manifestFlag) }