func TestQualifiedNames(t *testing.T) { for i, c := range []struct{ i, p, e string }{ {"a", "b", "a:b"}, {"a", ":b", "a:b"}, {"a", "", "a:"}, {"", "b", "b"}, {"", "", ""}, } { if got, want := profiles.QualifiedProfileName(c.i, c.p), c.e; got != want { t.Errorf("%d: got %v, want %v", i, got, want) } } for i, c := range []struct{ q, i, p string }{ {"aa:bb", "aa", "bb"}, {":bb", "", "bb"}, {"bb", "", "bb"}, {"", "", ""}, {":bb", "", "bb"}, } { gi, gp := profiles.SplitProfileName(c.q) if got, want := gi, c.i; got != want { t.Errorf("%d: got %v, want %v", i, got, want) } if got, want := gp, c.p; got != want { t.Errorf("%d: got %v, want %v", i, got, want) } } if got, want := profiles.QualifiedProfileName("new", "old:bar"), "new:bar"; got != want { t.Errorf("got %v, want %v", got, want) } }
func logResult(jirix *jiri.X, action string, mgr profiles.Manager, target profiles.Target, err error) { fmt.Fprintf(jirix.Stdout(), "%s: %s %s: ", action, profiles.QualifiedProfileName(mgr.Installer(), mgr.Name()), target) if err == nil { fmt.Fprintf(jirix.Stdout(), "success\n") } else { fmt.Fprintf(jirix.Stdout(), "%v\n", err) } }
// Register is used to register a profile manager. It is an error // to call Registerr more than once with the same name, though it // is possible to register the same Manager using different names. func Register(mgr profiles.Manager) { registry.Lock() defer registry.Unlock() qualifiedName := profiles.QualifiedProfileName(mgr.Installer(), mgr.Name()) if _, present := registry.managers[qualifiedName]; present { panic("a profile manager is already registered for: " + qualifiedName) } registry.managers[qualifiedName] = mgr }
// ensureAction ensures that the requested profile and target // is installed/uninstalled, installing/uninstalling it if and only if necessary. func ensureAction(jirix *jiri.X, pdb *profiles.DB, action profiles.Action, installer, profile string, root jiri.RelPath, target profiles.Target) error { verb := "" switch action { case profiles.Install: verb = "install" case profiles.Uninstall: verb = "uninstall" default: return fmt.Errorf("unrecognised action %v", action) } if jirix.Verbose() { fmt.Fprintf(jirix.Stdout(), "%s %v %s\n", verb, action, target) } if t := pdb.LookupProfileTarget(installer, profile, target); t != nil { if jirix.Verbose() { fmt.Fprintf(jirix.Stdout(), "%v %v is already %sed as %v\n", profile, target, verb, t) } return nil } mgr := LookupManager(profiles.QualifiedProfileName(installer, profile)) if mgr == nil { return fmt.Errorf("profile %v is not supported", profile) } version, err := mgr.VersionInfo().Select(target.Version()) if err != nil { return err } target.SetVersion(version) if jirix.Verbose() { fmt.Fprintf(jirix.Stdout(), "%s %s %s\n", verb, profile, target.DebugString()) } if action == profiles.Install { return mgr.Install(jirix, pdb, root, target) } return mgr.Uninstall(jirix, pdb, root, target) }
func ExampleManager() { pdb := profiles.NewDB() myInstaller := "myProject" myProfile := "myNewProfile" profileName := profiles.QualifiedProfileName(myInstaller, myProfile) var target profiles.Target init := func() { mgr := newProfileMgr(myInstaller, myProfile) profilesmanager.Register(mgr) flags := flag.NewFlagSet("example", flag.ContinueOnError) profiles.RegisterTargetAndEnvFlags(flags, &target) flags.Parse([]string{"--target=arm-linux@1", "--env=A=B,C=D", "--env=E=F"}) } init() profileRoot := jiri.NewRelPath("profiles") mgr := profilesmanager.LookupManager(profileName) if mgr == nil { panic("manager not found for: " + profileName) } jirix := &jiri.X{Context: tool.NewDefaultContext()} // Install myNewProfile for target. if err := mgr.Install(jirix, pdb, profileRoot, target); err != nil { panic("failed to find manager for: " + profileName) } fmt.Println(mgr.String()) filename := tmpFile() defer os.RemoveAll(filepath.Dir(filename)) if err := pdb.Write(jirix, "test", filename); err != nil { panic(err) } // Start with a new profile data base. pdb = profiles.NewDB() // Read the profile database. pdb.Read(jirix, filename) mgr = profilesmanager.LookupManager(profileName) if mgr == nil { panic("manager not found for: " + profileName) } fmt.Println(mgr.String()) mgr.Uninstall(jirix, pdb, profileRoot, target) fmt.Println(mgr.String()) fmt.Println(mgr.VersionInfo().Supported()) fmt.Println(mgr.VersionInfo().Default()) // Output: // Profile: myNewProfile: installed // [arm-linux@1] // // Profile: myNewProfile: installed // [arm-linux@1] // // Profile: myNewProfile: not installed // // [4 3 2] // 3 }