github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/cmd/snap/notes.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 main
    21  
    22  import (
    23  	"fmt"
    24  	"strings"
    25  
    26  	"github.com/snapcore/snapd/client"
    27  	"github.com/snapcore/snapd/i18n"
    28  	"github.com/snapcore/snapd/snap"
    29  )
    30  
    31  func getPriceString(prices map[string]float64, suggestedCurrency, status string) string {
    32  	price, currency, err := getPrice(prices, suggestedCurrency)
    33  
    34  	// If there are no prices, then the snap is free
    35  	if err != nil {
    36  		return ""
    37  	}
    38  
    39  	// If the snap is priced, but has been purchased
    40  	if status == "available" {
    41  		return i18n.G("bought")
    42  	}
    43  
    44  	return formatPrice(price, currency)
    45  }
    46  
    47  func formatPrice(val float64, currency string) string {
    48  	return fmt.Sprintf("%.2f%s", val, currency)
    49  }
    50  
    51  // Notes encapsulate everything that might be interesting about a
    52  // snap, in order to present a brief summary of it.
    53  type Notes struct {
    54  	SnapType         snap.Type
    55  	Private          bool
    56  	DevMode          bool
    57  	JailMode         bool
    58  	Classic          bool
    59  	TryMode          bool
    60  	Disabled         bool
    61  	Broken           bool
    62  	IgnoreValidation bool
    63  	InCohort         bool
    64  	Health           string
    65  	Price            string
    66  }
    67  
    68  func NotesFromChannelSnapInfo(ref *snap.ChannelSnapInfo) *Notes {
    69  	return &Notes{
    70  		DevMode: ref.Confinement == client.DevModeConfinement,
    71  		Classic: ref.Confinement == client.ClassicConfinement,
    72  	}
    73  }
    74  
    75  func NotesFromRemote(snp *client.Snap, resInfo *client.ResultInfo) *Notes {
    76  	notes := &Notes{
    77  		Private:  snp.Private,
    78  		DevMode:  snp.Confinement == client.DevModeConfinement,
    79  		Classic:  snp.Confinement == client.ClassicConfinement,
    80  		SnapType: snap.Type(snp.Type),
    81  	}
    82  	if resInfo != nil {
    83  		notes.Price = getPriceString(snp.Prices, resInfo.SuggestedCurrency, snp.Status)
    84  	}
    85  
    86  	return notes
    87  }
    88  
    89  func NotesFromLocal(snp *client.Snap) *Notes {
    90  	var health string
    91  	if snp.Health != nil {
    92  		health = snp.Health.Status
    93  	}
    94  	return &Notes{
    95  		SnapType:         snap.Type(snp.Type),
    96  		Private:          snp.Private,
    97  		DevMode:          snp.DevMode,
    98  		Classic:          !snp.JailMode && (snp.Confinement == client.ClassicConfinement),
    99  		JailMode:         snp.JailMode,
   100  		TryMode:          snp.TryMode,
   101  		Disabled:         snp.Status != client.StatusActive,
   102  		Broken:           snp.Broken != "",
   103  		IgnoreValidation: snp.IgnoreValidation,
   104  		InCohort:         snp.CohortKey != "",
   105  		Health:           health,
   106  	}
   107  }
   108  
   109  func (n *Notes) String() string {
   110  	if n == nil {
   111  		return ""
   112  	}
   113  	var ns []string
   114  
   115  	switch n.SnapType {
   116  	case "", snap.TypeApp:
   117  		// nothing
   118  	case snap.TypeOS:
   119  		ns = append(ns, "core")
   120  	default:
   121  		ns = append(ns, string(n.SnapType))
   122  	}
   123  	if n.Disabled {
   124  		// TRANSLATORS: if possible, a single short word
   125  		ns = append(ns, i18n.G("disabled"))
   126  	}
   127  
   128  	if n.Price != "" {
   129  		ns = append(ns, n.Price)
   130  	}
   131  
   132  	if n.DevMode {
   133  		ns = append(ns, "devmode")
   134  	}
   135  
   136  	if n.JailMode {
   137  		ns = append(ns, "jailmode")
   138  	}
   139  
   140  	if n.Classic {
   141  		ns = append(ns, "classic")
   142  	}
   143  
   144  	if n.Private {
   145  		// TRANSLATORS: if possible, a single short word
   146  		ns = append(ns, i18n.G("private"))
   147  	}
   148  
   149  	if n.TryMode {
   150  		ns = append(ns, "try")
   151  	}
   152  
   153  	if n.Broken {
   154  		// TRANSLATORS: if possible, a single short word
   155  		ns = append(ns, i18n.G("broken"))
   156  	}
   157  
   158  	if n.IgnoreValidation {
   159  		ns = append(ns, i18n.G("ignore-validation"))
   160  	}
   161  
   162  	if n.InCohort {
   163  		ns = append(ns, i18n.G("in-cohort"))
   164  	}
   165  	if n.Health != "" && n.Health != "okay" {
   166  		ns = append(ns, n.Health)
   167  	}
   168  
   169  	if len(ns) == 0 {
   170  		return "-"
   171  	}
   172  
   173  	return strings.Join(ns, ",")
   174  }