github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/client/aliases.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016 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 client
    21  
    22  import (
    23  	"bytes"
    24  	"encoding/json"
    25  )
    26  
    27  // aliasAction represents an action performed on aliases.
    28  // With action "unalias" if Snap and Alias are set to the same value,
    29  // snapd will check if what is referred to is indeed a snap or an alias.
    30  type aliasAction struct {
    31  	Action string `json:"action"`
    32  	Snap   string `json:"snap,omitempty"`
    33  	App    string `json:"app,omitempty"`
    34  	Alias  string `json:"alias,omitempty"`
    35  }
    36  
    37  // performAliasAction performs a single action on aliases.
    38  func (client *Client) performAliasAction(sa *aliasAction) (changeID string, err error) {
    39  	b, err := json.Marshal(sa)
    40  	if err != nil {
    41  		return "", err
    42  	}
    43  	return client.doAsync("POST", "/v2/aliases", nil, nil, bytes.NewReader(b))
    44  }
    45  
    46  // Alias sets up a manual alias from alias to app in snapName.
    47  func (client *Client) Alias(snapName, app, alias string) (changeID string, err error) {
    48  	return client.performAliasAction(&aliasAction{
    49  		Action: "alias",
    50  		Snap:   snapName,
    51  		App:    app,
    52  		Alias:  alias,
    53  	})
    54  }
    55  
    56  // // DisableAllAliases disables all aliases of a snap, removing all manual ones.
    57  func (client *Client) DisableAllAliases(snapName string) (changeID string, err error) {
    58  	return client.performAliasAction(&aliasAction{
    59  		Action: "unalias",
    60  		Snap:   snapName,
    61  	})
    62  }
    63  
    64  // RemoveManualAlias removes a manual alias.
    65  func (client *Client) RemoveManualAlias(alias string) (changeID string, err error) {
    66  	return client.performAliasAction(&aliasAction{
    67  		Action: "unalias",
    68  		Alias:  alias,
    69  	})
    70  }
    71  
    72  // Unalias tears down a manual alias or disables all aliases of a snap (removing all manual ones)
    73  func (client *Client) Unalias(aliasOrSnap string) (changeID string, err error) {
    74  	return client.performAliasAction(&aliasAction{
    75  		Action: "unalias",
    76  		Snap:   aliasOrSnap,
    77  		Alias:  aliasOrSnap,
    78  	})
    79  }
    80  
    81  // Prefer enables all aliases of a snap in preference to conflicting aliases
    82  // of other snaps whose aliases will be disabled (removed for manual ones).
    83  func (client *Client) Prefer(snapName string) (changeID string, err error) {
    84  	return client.performAliasAction(&aliasAction{
    85  		Action: "prefer",
    86  		Snap:   snapName,
    87  	})
    88  }
    89  
    90  // AliasStatus represents the status of an alias.
    91  type AliasStatus struct {
    92  	Command string `json:"command"`
    93  	Status  string `json:"status"`
    94  	Manual  string `json:"manual,omitempty"`
    95  	Auto    string `json:"auto,omitempty"`
    96  }
    97  
    98  // Aliases returns a map snap -> alias -> AliasStatus for all snaps and aliases in the system.
    99  func (client *Client) Aliases() (allStatuses map[string]map[string]AliasStatus, err error) {
   100  	_, err = client.doSync("GET", "/v2/aliases", nil, nil, nil, &allStatuses)
   101  	return
   102  }