func TestBackwardsCompatibility(t *testing.T) { profiles.Clear() getProfiles := func() []*profiles.Profile { db := []*profiles.Profile{} names := profiles.Profiles() sort.Strings(names) for _, name := range names { db = append(db, profiles.LookupProfile(name)) } return db } ctx := tool.NewDefaultContext() if err := profiles.Read(ctx, "./testdata/legacy.xml"); err != nil { t.Fatal(err) } if got, want := profiles.SchemaVersion(), profiles.Original; got != want { t.Errorf("got %v, want %v", got, want) } oprofiles := getProfiles() if got, want := len(oprofiles), 5; got != want { t.Errorf("got %v, want %v", got, want) } filename := tmpFile() defer os.RemoveAll(filepath.Dir(filename)) var t1 profiles.Target t1.Set("tag=cpu,os") profiles.AddProfileTarget("__first", t1) if err := profiles.Write(ctx, filename); err != nil { t.Fatal(err) } if err := profiles.Read(ctx, filename); err != nil { t.Fatal(err) } if got, want := profiles.SchemaVersion(), profiles.V3; got != want { t.Errorf("got %v, want %v", got, want) } nprofiles := getProfiles() if got, want := len(nprofiles), 6; got != want { t.Errorf("got %v, want %v", got, want) } if got, want := nprofiles[0].Name, "__first"; got != want { t.Errorf("got %v, want %v", got, want) } for i, v := range nprofiles[1:] { if got, want := v.Name, oprofiles[i].Name; got != want { t.Errorf("got %v, want %v", got, want) } } }
func runRecreate(env *cmdline.Env, args []string) error { ctx := tool.NewContextFromEnv(env) 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() } prefix := "jiri v23-profile install" for _, name := range profileNames { profile := profiles.LookupProfile(name) if profile == nil { return fmt.Errorf("Profile %v is not installed", name) } for _, target := range profile.Targets() { fmt.Fprintf(ctx.Stdout(), "%s --target=%s", prefix, target) cmdEnv := target.CommandLineEnv() if len(cmdEnv.Vars) > 0 { fmt.Fprintf(ctx.Stdout(), " --env=\"%s\"", strings.Join(cmdEnv.Vars, ",")) } fmt.Fprintf(ctx.Stdout(), " %s\n", name) } } return nil }
func TestRead(t *testing.T) { profiles.Clear() ctx := tool.NewDefaultContext() if err := profiles.Read(ctx, "./testdata/m1.xml"); err != nil { t.Fatal(err) } cmp := func(a, b []string) bool { if len(a) != len(b) { return false } for i, s := range a { if s != b[i] { return false } } return true } names := profiles.Profiles() sort.Strings(names) if got, want := names, []string{"a", "b"}; !cmp(got, want) { t.Errorf("got %v, want %v", got, want) } p := profiles.LookupProfile("a") if got, want := p.Targets()[0].Tag(), "t1"; got != want { t.Errorf("got %v, want %v", got, want) } if got, want := p.Targets()[0].OS(), "os1"; got != want { t.Errorf("got %v, want %v", got, want) } if got, want := p.Targets()[1].Version(), "bar"; got != want { t.Errorf("got %v, want %v", got, want) } }
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 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 runEnv(env *cmdline.Env, args []string) error { if len(profileFlag) == 0 { return fmt.Errorf("no profile was specified using --profile") } ctx := tool.NewContextFromEnv(env) if err := profiles.Read(ctx, manifestFlag); err != nil { return fmt.Errorf("Failed to read manifest: %v", err) } profile := profiles.LookupProfile(profileFlag) if profile == nil { return fmt.Errorf("profile %q is not installed", profileFlag) } target := profiles.FindTarget(profile.Targets(), &targetFlag) if target == nil { return fmt.Errorf("target %q is not installed for profile %q", targetFlag, profileFlag) } vars := envvar.SliceToMap(target.Env.Vars) buf := bytes.Buffer{} if len(args) == 0 { for k, v := range vars { buf.WriteString(fmt.Sprintf("%s=%q ", k, v)) } for k, fn := range pseudoVariables { buf.WriteString(fmt.Sprintf("%s=%q ", k, fn(target))) } } else { for _, arg := range args { name := strings.TrimSuffix(arg, "=") trimmed := name != arg for k, fn := range pseudoVariables { if k == name { buf.WriteString(expr(k, fn(target), trimmed)) } } for k, v := range vars { if k == name { buf.WriteString(expr(k, v, trimmed)) } } } } fmt.Fprintf(ctx.Stdout(), strings.TrimSuffix(buf.String(), " ")+"\n") return nil }
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 }