// Copyright (c) 2013 Perforce Software. All rights reserved. // // Some higher level operations on the "p4 client" command package p4go import ( "bytes" "strings" "unicode" "unicode/utf8" ) // High level operations around the "p4 client" command type ClientDAO interface { // p4 client -o GetClient(id string) (map[string]string, error) // p4 client -i SaveClient(id string, client map[string]string) error } func (dao *p4DaoHandle) GetClient(id string) (map[string]string, error) { dao.Clear() dao.GetClientApi().SetArgv([]string{"-o", id}) dao.GetClientApi().Run("client", dao.GetClientUser()) if dao.HasError() { return nil, dao.CreateError() } client, err := mapTagOutput(dao.GetClientUser().GetOutputInfo()) if err != nil { return nil, err } return client, nil } func (dao *p4DaoHandle) SaveClient(id string, client map[string]string) error { // Just to keep it clean, we create a new map with only the "normal" values // to add. We'll add the id after this just to make sure it's set correctly toSave := map[string]string{} mappings := make([][]string, 0) // save all view mappings separately for key, value := range client { // I'm sure that all valid keys are encoded in UTF-8 ... hm... firstRune, runeSize := utf8.DecodeRuneInString(key) if !strings.HasPrefix(key, "View") && runeSize == 1 && unicode.IsUpper(firstRune) { toSave[key] = value } else if strings.HasPrefix(key, "View") { mapping, err := splitMapping(value) if err != nil { return err } mappings = append(mappings, mapping) } } // The client name comes back as "client" not "Client" in the tagged output toSave["Client"] = id // Write out the simpler key value pairs spec := bytes.Buffer{} appendSpecValues(&spec, toSave) spec.WriteString("View:\n") for _, mapping := range mappings { left := enquoteIfNeeded(mapping[0]) right := enquoteIfNeeded(mapping[1]) spec.WriteRune('\t') spec.WriteString(left) spec.WriteRune(' ') spec.WriteString(right) spec.WriteRune('\n') } dao.Clear() dao.GetClientApi().SetArgv([]string{"-i"}) dao.GetClientUser().SetInputData(spec.String()) dao.GetClientApi().Run("client", dao.GetClientUser()) if dao.HasError() { return dao.CreateError() } return nil }