github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/snap/types.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-2015 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package snap
    21  
    22  import (
    23  	"encoding/json"
    24  	"fmt"
    25  )
    26  
    27  // Type represents the kind of snap (app, core, gadget, os, kernel, snapd)
    28  type Type string
    29  
    30  // The various types of snap parts we support
    31  const (
    32  	TypeApp    Type = "app"
    33  	TypeGadget Type = "gadget"
    34  	TypeKernel Type = "kernel"
    35  	TypeBase   Type = "base"
    36  	TypeSnapd  Type = "snapd"
    37  
    38  	// FIXME: this really should be TypeCore
    39  	TypeOS Type = "os"
    40  )
    41  
    42  // This is the sort order from least important to most important for
    43  // types. On e.g. firstboot this will be used to order the snaps this
    44  // way.
    45  var typeOrder = map[Type]int{
    46  	TypeApp:    50,
    47  	TypeGadget: 40,
    48  	TypeBase:   30,
    49  	TypeKernel: 20,
    50  	TypeOS:     10,
    51  	TypeSnapd:  0,
    52  }
    53  
    54  func (m Type) SortsBefore(other Type) bool {
    55  	return typeOrder[m] < typeOrder[other]
    56  }
    57  
    58  // UnmarshalJSON sets *m to a copy of data.
    59  func (m *Type) UnmarshalJSON(data []byte) error {
    60  	var str string
    61  	if err := json.Unmarshal(data, &str); err != nil {
    62  		return err
    63  	}
    64  
    65  	return m.fromString(str)
    66  }
    67  
    68  // UnmarshalYAML so Type implements yaml's Unmarshaler interface
    69  func (m *Type) UnmarshalYAML(unmarshal func(interface{}) error) error {
    70  	var str string
    71  	if err := unmarshal(&str); err != nil {
    72  		return err
    73  	}
    74  
    75  	return m.fromString(str)
    76  }
    77  
    78  // fromString converts str to Type and sets *m to it if validations pass
    79  func (m *Type) fromString(str string) error {
    80  	t := Type(str)
    81  
    82  	// this is a workaround as the store sends "application" but snappy uses
    83  	// "app" for TypeApp
    84  	if str == "application" {
    85  		t = TypeApp
    86  	}
    87  
    88  	if t != TypeApp && t != TypeGadget && t != TypeOS && t != TypeKernel && t != TypeBase && t != TypeSnapd {
    89  		return fmt.Errorf("invalid snap type: %q", str)
    90  	}
    91  
    92  	*m = t
    93  
    94  	return nil
    95  }
    96  
    97  // ConfinementType represents the kind of confinement supported by the snap
    98  // (devmode only, or strict confinement)
    99  type ConfinementType string
   100  
   101  // The various confinement types we support
   102  const (
   103  	DevModeConfinement ConfinementType = "devmode"
   104  	ClassicConfinement ConfinementType = "classic"
   105  	StrictConfinement  ConfinementType = "strict"
   106  )
   107  
   108  // UnmarshalJSON sets *confinementType to a copy of data, assuming validation passes
   109  func (confinementType *ConfinementType) UnmarshalJSON(data []byte) error {
   110  	var s string
   111  	if err := json.Unmarshal(data, &s); err != nil {
   112  		return err
   113  	}
   114  
   115  	return confinementType.fromString(s)
   116  }
   117  
   118  // UnmarshalYAML so ConfinementType implements yaml's Unmarshaler interface
   119  func (confinementType *ConfinementType) UnmarshalYAML(unmarshal func(interface{}) error) error {
   120  	var s string
   121  	if err := unmarshal(&s); err != nil {
   122  		return err
   123  	}
   124  
   125  	return confinementType.fromString(s)
   126  }
   127  
   128  func (confinementType *ConfinementType) fromString(str string) error {
   129  	c := ConfinementType(str)
   130  	if c != DevModeConfinement && c != ClassicConfinement && c != StrictConfinement {
   131  		return fmt.Errorf("invalid confinement type: %q", str)
   132  	}
   133  
   134  	*confinementType = c
   135  
   136  	return nil
   137  }
   138  
   139  type ServiceStopReason string
   140  
   141  const (
   142  	StopReasonRefresh ServiceStopReason = "refresh"
   143  	StopReasonRemove  ServiceStopReason = "remove"
   144  	StopReasonDisable ServiceStopReason = "disable"
   145  	StopReasonOther   ServiceStopReason = ""
   146  )
   147  
   148  // DaemonScope represents the scope of the daemon running under systemd
   149  type DaemonScope string
   150  
   151  const (
   152  	SystemDaemon DaemonScope = "system"
   153  	UserDaemon   DaemonScope = "user"
   154  )
   155  
   156  // UnmarshalJSON sets *daemonScope to a copy of data, assuming validation passes
   157  func (daemonScope *DaemonScope) UnmarshalJSON(data []byte) error {
   158  	var s string
   159  	if err := json.Unmarshal(data, &s); err != nil {
   160  		return err
   161  	}
   162  
   163  	return daemonScope.fromString(s)
   164  }
   165  
   166  // UnmarshalYAML so DaemonScope implements yaml's Unmarshaler interface
   167  func (daemonScope *DaemonScope) UnmarshalYAML(unmarshal func(interface{}) error) error {
   168  	var s string
   169  	if err := unmarshal(&s); err != nil {
   170  		return err
   171  	}
   172  
   173  	return daemonScope.fromString(s)
   174  }
   175  
   176  func (daemonScope *DaemonScope) fromString(str string) error {
   177  	d := DaemonScope(str)
   178  	if d != SystemDaemon && d != UserDaemon {
   179  		return fmt.Errorf("invalid daemon scope: %q", str)
   180  	}
   181  
   182  	*daemonScope = d
   183  	return nil
   184  }