本文整理汇总了Golang中github.com/snapcore/snapd/overlord/state.State.Get方法的典型用法代码示例。如果您正苦于以下问题:Golang State.Get方法的具体用法?Golang State.Get怎么用?Golang State.Get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/snapcore/snapd/overlord/state.State
的用法示例。
在下文中一共展示了State.Get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: NewUser
// NewUser tracks a new authenticated user and saves its details in the state
func NewUser(st *state.State, username, macaroon string, discharges []string) (*UserState, error) {
var authStateData AuthState
err := st.Get("auth", &authStateData)
if err == state.ErrNoState {
authStateData = AuthState{}
} else if err != nil {
return nil, err
}
sort.Strings(discharges)
authStateData.LastID++
authenticatedUser := UserState{
ID: authStateData.LastID,
Username: username,
Macaroon: macaroon,
Discharges: discharges,
StoreMacaroon: macaroon,
StoreDischarges: discharges,
}
authStateData.Users = append(authStateData.Users, authenticatedUser)
st.Set("auth", authStateData)
return &authenticatedUser, nil
}
示例2: checkAliasConflict
func checkAliasConflict(st *state.State, snapName, alias string) error {
// check against snaps
var snapNames map[string]*json.RawMessage
err := st.Get("snaps", &snapNames)
if err != nil && err != state.ErrNoState {
return err
}
for name := range snapNames {
if name == alias || strings.HasPrefix(alias, name+".") {
return &aliasConflictError{
Alias: alias,
Snap: snapName,
Reason: fmt.Sprintf("it conflicts with the command namespace of installed snap %q", name),
}
}
}
// check against aliases
return checkAgainstEnabledAliases(st, func(otherAlias, otherSnap string) error {
if otherAlias == alias {
return &aliasConflictError{
Alias: alias,
Snap: snapName,
Reason: fmt.Sprintf("already enabled for %q", otherSnap),
}
}
return nil
})
}
示例3: Apply
// Apply applies any necessary patches to update the provided state to
// conventions required by the current patch level of the system.
func Apply(s *state.State) error {
var stateLevel int
s.Lock()
err := s.Get("patch-level", &stateLevel)
s.Unlock()
if err != nil && err != state.ErrNoState {
return err
}
if stateLevel == Level {
// already at right level, nothing to do
return nil
}
if stateLevel > Level {
return fmt.Errorf("cannot downgrade: snapd is too old for the current system state (patch level %d)", stateLevel)
}
level := stateLevel
for level < Level {
logger.Noticef("Patching system state from level %d to %d", level, level+1)
patch := patches[level+1]
if patch == nil {
return fmt.Errorf("cannot upgrade: snapd is too new for the current system state (patch level %d)", level)
}
err := applyOne(patch, s, level)
if err != nil {
logger.Noticef("Cannnot patch: %v", err)
return fmt.Errorf("cannot patch system state from level %d to %d: %v", level, level+1, err)
}
level++
}
return nil
}
示例4: CheckMacaroon
// CheckMacaroon returns the UserState for the given macaroon/discharges credentials
func CheckMacaroon(st *state.State, macaroon string, discharges []string) (*UserState, error) {
var authStateData AuthState
err := st.Get("auth", &authStateData)
if err != nil {
return nil, ErrInvalidAuth
}
NextUser:
for _, user := range authStateData.Users {
if user.Macaroon != macaroon {
continue
}
if len(user.Discharges) != len(discharges) {
continue
}
// sort discharges (stored users' discharges are already sorted)
sort.Strings(discharges)
for i, d := range user.Discharges {
if d != discharges[i] {
continue NextUser
}
}
return &user, nil
}
return nil, ErrInvalidAuth
}
示例5: patch1
// patch1 adds the snap type and the current revision to the snap state.
func patch1(s *state.State) error {
var stateMap map[string]*snapstate.SnapState
err := s.Get("snaps", &stateMap)
if err == state.ErrNoState {
return nil
}
if err != nil {
return err
}
for snapName, snapState := range stateMap {
seq := snapState.Sequence
if len(seq) == 0 {
continue
}
typ := snap.TypeApp
snapInfo, err := readInfo(snapName, seq[len(seq)-1])
if err != nil {
logger.Noticef("Recording type for snap %q: cannot retrieve info, assuming it's a app: %v", snapName, err)
} else {
logger.Noticef("Recording type for snap %q: setting to %q", snapName, snapInfo.Type)
typ = snapInfo.Type
}
snapState.SetType(typ)
snapState.Current = seq[len(seq)-1].Revision
}
s.Set("snaps", stateMap)
return nil
}
示例6: Init
// Init initializes an empty state to the current implemented patch level.
func Init(s *state.State) {
s.Lock()
defer s.Unlock()
if s.Get("patch-level", new(int)) != state.ErrNoState {
panic("internal error: expected empty state, attempting to override patch-level without actual patching")
}
s.Set("patch-level", Level)
}
示例7: patch2
// patch2:
// - migrates SnapSetup.Name to SnapSetup.SideInfo.RealName
// - backfills SnapState.{Sequence,Candidate}.RealName if its missing
func patch2(s *state.State) error {
var stateMap map[string]*OldSnapState
err := s.Get("snaps", &stateMap)
if err == state.ErrNoState {
return nil
}
if err != nil {
return err
}
// migrate SnapSetup in all tasks:
// - the new SnapSetup uses SideInfo, backfil from Candidate
// - also move SnapSetup.{Name,Revision} into SnapSetup.SideInfo.{RealName,Revision}
var oldSS OldSnapSetup
var newSS snapstate.SnapSetup
for _, t := range s.Tasks() {
err := t.Get("snap-setup", &oldSS)
if err == state.ErrNoState {
continue
}
if err != nil && err != state.ErrNoState {
return err
}
// some things stay the same
newSS.Channel = oldSS.Channel
newSS.Flags = oldSS.Flags
newSS.SnapPath = oldSS.SnapPath
newSS.DownloadInfo = oldSS.DownloadInfo
newSS.SideInfo = oldSS.SideInfo
// ... and some change
if newSS.SideInfo == nil {
newSS.SideInfo = &snap.SideInfo{}
if snapst, ok := stateMap[oldSS.Name]; ok && snapst.Candidate != nil {
newSS.SideInfo = snapst.Candidate
}
}
if newSS.SideInfo.RealName == "" {
newSS.SideInfo.RealName = oldSS.Name
}
if newSS.SideInfo.Revision.Unset() {
newSS.SideInfo.Revision = oldSS.Revision
}
t.Set("snap-setup", &newSS)
}
// backfill snapstate.SnapState.{Sequence,Candidate} with RealName
// (if that is missing, was missing for e.g. sideloaded snaps)
for snapName, snapState := range stateMap {
for _, si := range snapState.Sequence {
setRealName(si, snapName)
}
}
s.Set("snaps", stateMap)
return nil
}
示例8: CheckMacaroon
// CheckMacaroon returns the UserState for the given macaroon/discharges credentials
func CheckMacaroon(st *state.State, macaroon string, discharges []string) (*UserState, error) {
var authStateData AuthState
err := st.Get("auth", &authStateData)
if err != nil {
return nil, ErrInvalidAuth
}
snapdMacaroon, err := MacaroonDeserialize(macaroon)
if err != nil {
return nil, ErrInvalidAuth
}
// attempt snapd macaroon verification
if snapdMacaroon.Location() == snapdMacaroonLocation {
// no caveats to check so far
check := func(caveat string) error { return nil }
// ignoring discharges, unused for snapd macaroons atm
err = snapdMacaroon.Verify(authStateData.MacaroonKey, check, nil)
if err != nil {
return nil, ErrInvalidAuth
}
macaroonID := snapdMacaroon.Id()
userID, err := strconv.Atoi(macaroonID)
if err != nil {
return nil, ErrInvalidAuth
}
user, err := User(st, userID)
if err != nil {
return nil, ErrInvalidAuth
}
if macaroon != user.Macaroon {
return nil, ErrInvalidAuth
}
return user, nil
}
// if macaroon is not a snapd macaroon, fallback to previous token-style check
NextUser:
for _, user := range authStateData.Users {
if user.Macaroon != macaroon {
continue
}
if len(user.Discharges) != len(discharges) {
continue
}
// sort discharges (stored users' discharges are already sorted)
sort.Strings(discharges)
for i, d := range user.Discharges {
if d != discharges[i] {
continue NextUser
}
}
return &user, nil
}
return nil, ErrInvalidAuth
}
示例9: getConns
func getConns(st *state.State) (map[string]connState, error) {
// Get information about connections from the state
var conns map[string]connState
err := st.Get("conns", &conns)
if err != nil && err != state.ErrNoState {
return nil, fmt.Errorf("cannot obtain data about existing connections: %s", err)
}
if conns == nil {
conns = make(map[string]connState)
}
return conns, nil
}
示例10: patch2
// patch2:
// - migrates SnapSetup.Name to SnapSetup.SideInfo.RealName
// - backfills SnapState.{Sequence,Candidate}.RealName if its missing
func patch2(s *state.State) error {
var oldStateMap map[string]*patch1SnapState
err := s.Get("snaps", &oldStateMap)
if err == state.ErrNoState {
return nil
}
if err != nil {
return err
}
newStateMap := make(map[string]*patch2SnapState, len(oldStateMap))
for key, oldSnapState := range oldStateMap {
newStateMap[key] = patch2SnapStateFromPatch1(oldSnapState, key)
}
// migrate SnapSetup in all tasks:
// - the new SnapSetup uses SideInfo, backfil from Candidate
// - also move SnapSetup.{Name,Revision} into SnapSetup.SideInfo.{RealName,Revision}
var oldSS patch1SnapSetup
for _, t := range s.Tasks() {
var newSS patch2SnapSetup
err := t.Get("snap-setup", &oldSS)
if err == state.ErrNoState {
continue
}
if err != nil && err != state.ErrNoState {
return err
}
// some things stay the same
newSS.Channel = oldSS.Channel
newSS.Flags = patch2Flags(oldSS.Flags)
newSS.SnapPath = oldSS.SnapPath
// ... and some change
newSS.SideInfo = &patch2SideInfo{}
if snapst, ok := oldStateMap[oldSS.Name]; ok && snapst.Candidate != nil {
newSS.SideInfo = patch2SideInfoFromPatch1(snapst.Candidate, oldSS.Name)
}
if newSS.SideInfo.RealName == "" {
newSS.SideInfo.RealName = oldSS.Name
}
if newSS.SideInfo.Revision.Unset() {
newSS.SideInfo.Revision = oldSS.Revision
}
t.Set("snap-setup", &newSS)
}
s.Set("snaps", newStateMap)
return nil
}
示例11: NewTransaction
// NewTransaction creates a new configuration transaction initialized with the given state.
//
// The provided state must be locked by the caller.
func NewTransaction(st *state.State) *Transaction {
transaction := &Transaction{state: st}
transaction.changes = make(map[string]map[string]interface{})
// Record the current state of the map containing the config of every snap
// in the system. We'll use it for this transaction.
err := st.Get("config", &transaction.pristine)
if err == state.ErrNoState {
transaction.pristine = make(map[string]map[string]*json.RawMessage)
} else if err != nil {
panic(fmt.Errorf("internal error: cannot unmarshal configuration: %v", err))
}
return transaction
}
示例12: SetDevice
// SetDevice updates the device details in the state.
func SetDevice(st *state.State, device *DeviceState) error {
var authStateData AuthState
err := st.Get("auth", &authStateData)
if err == state.ErrNoState {
authStateData = AuthState{}
} else if err != nil {
return err
}
authStateData.Device = device
st.Set("auth", authStateData)
return nil
}
示例13: User
// User returns a user from the state given its ID
func User(st *state.State, id int) (*UserState, error) {
var authStateData AuthState
err := st.Get("auth", &authStateData)
if err != nil {
return nil, err
}
for _, user := range authStateData.Users {
if user.ID == id {
return &user, nil
}
}
return nil, fmt.Errorf("invalid user")
}
示例14: All
// All retrieves return a map from name to SnapState for all current snaps in the system state.
func All(s *state.State) (map[string]*SnapState, error) {
// XXX: result is a map because sideloaded snaps carry no name
// atm in their sideinfos
var stateMap map[string]*SnapState
if err := s.Get("snaps", &stateMap); err != nil && err != state.ErrNoState {
return nil, err
}
curStates := make(map[string]*SnapState, len(stateMap))
for snapName, snapState := range stateMap {
if snapState.HasCurrent() {
curStates[snapName] = snapState
}
}
return curStates, nil
}
示例15: Device
// Device returns the device details from the state.
func Device(st *state.State) (*DeviceState, error) {
var authStateData AuthState
err := st.Get("auth", &authStateData)
if err == state.ErrNoState {
return &DeviceState{}, nil
} else if err != nil {
return nil, err
}
if authStateData.Device == nil {
return &DeviceState{}, nil
}
return authStateData.Device, nil
}