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 runUninstall(env *cmdline.Env, args []string) error { ctx := tool.NewContextFromEnv(env) if err := initCommand(ctx, args); err != nil { return err } if allFlag && targetFlag.IsSet() { return fmt.Errorf("don't specify a target in conjunction with --all") } if allFlag { for _, name := range args { profile := profiles.LookupProfile(name) mgr := profiles.LookupManager(name) if profile == nil || mgr == nil { continue } mgr.SetRoot(rootDir) for _, target := range profile.Targets() { if err := mgr.Uninstall(ctx, *target); err != nil { logResult(ctx, "Uninstall", mgr, *target, err) return err } logResult(ctx, "Uninstall", mgr, *target, nil) } } } else { applyCommand(args, env, ctx, targetFlag, func(mgr profiles.Manager, ctx *tool.Context, target profiles.Target) error { err := mgr.Uninstall(ctx, target) logResult(ctx, "Uninstall", mgr, target, err) return err }) } return profiles.Write(ctx, manifestFlag) }
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 applyCommand(names []string, env *cmdline.Env, ctx *tool.Context, target profiles.Target, fn func(profiles.Manager, *tool.Context, profiles.Target) error) error { for _, n := range names { mgr := profiles.LookupManager(n) version, err := mgr.VersionInfo().Select(target.Version()) if err != nil { return err } target.SetVersion(version) mgr.SetRoot(rootDir) if err := fn(mgr, ctx, target); err != nil { return err } } return nil }
func initCommand(ctx *tool.Context, args []string) error { if len(args) == 0 { return fmt.Errorf("no profiles specified") } for _, n := range args { if mgr := profiles.LookupManager(n); mgr == nil { return fmt.Errorf("profile %v is not available, use \"list --available\" to see the list of available profiles", n) } } if err := profiles.Read(ctx, manifestFlag); err != nil { fmt.Fprintf(ctx.Stderr(), "Failed to read manifest: %v", err) return err } return nil }
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) }
func ExampleManager() { myProfile := "myNewProfile" var target profiles.Target init := func() { profiles.Register(myProfile, newProfile(myProfile)) flags := flag.NewFlagSet("example", flag.ContinueOnError) profiles.RegisterTargetAndEnvFlags(flags, &target) flags.Parse([]string{"--target=myTarget=arm-linux", "--env=A=B,C=D", "--env=E=F"}) } init() mgr := profiles.LookupManager(myProfile) if mgr == nil { panic("manager not found for: " + myProfile) } ctx := tool.NewDefaultContext() // Install myNewProfile for target. if err := mgr.Install(ctx, target); err != nil { panic("failed to find manager for: " + myProfile) } fmt.Println(mgr.String()) filename := tmpFile() defer os.RemoveAll(filepath.Dir(filename)) if err := profiles.Write(ctx, filename); err != nil { panic(err) } // Clear the profile manifest information in order to mimic starting // a new process and reading the manifest file. profiles.Clear() // Read the profile manifest. profiles.Read(ctx, filename) mgr = profiles.LookupManager(myProfile) if mgr == nil { panic("manager not found for: " + myProfile) } fmt.Println(mgr.String()) mgr.Uninstall(ctx, target) fmt.Println(mgr.String()) fmt.Println(mgr.VersionInfo().Supported()) fmt.Println(mgr.VersionInfo().Default()) // Output: // Profile: myNewProfile: installed // [myTarget=arm-linux@] // // Profile: myNewProfile: installed // [myTarget=arm-linux@] // // Profile: myNewProfile: uninstalled // // [4 3 2] // 3 }