github.com/chipaca/snappy@v0.0.0-20210104084008-1f06296fe8ad/cmd/snap/cmd_create_cohort.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2019 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  	"github.com/jessevdk/go-flags"
    24  	"gopkg.in/yaml.v2"
    25  
    26  	"github.com/snapcore/snapd/i18n"
    27  )
    28  
    29  var shortCreateCohortHelp = i18n.G("Create cohort keys for a set of snaps")
    30  var longCreateCohortHelp = i18n.G(`
    31  The create-cohort command creates a set of cohort keys for a given set of snaps.
    32  
    33  A cohort is a view or snapshot of a snap's "channel map" at a given point in
    34  time that fixes the set of revisions for the snap given other constraints
    35  (e.g. channel or architecture). The cohort is then identified by an opaque
    36  per-snap key that works across systems. Installations or refreshes of the snap
    37  using a given cohort key would use a fixed revision for up to 90 days, after
    38  which a new set of revisions would be fixed under that same cohort key and a
    39  new 90 days window started.
    40  `)
    41  
    42  type cmdCreateCohort struct {
    43  	clientMixin
    44  	Positional struct {
    45  		Snaps []anySnapName `positional-arg-name:"<snap>" required:"1"`
    46  	} `positional-args:"yes" required:"yes"`
    47  }
    48  
    49  func init() {
    50  	addCommand("create-cohort", shortCreateCohortHelp, longCreateCohortHelp, func() flags.Commander { return &cmdCreateCohort{} }, nil, nil)
    51  }
    52  
    53  // output should be YAML, so we use these two as helpers to get that done easy
    54  type cohortInnerYAML struct {
    55  	CohortKey string `yaml:"cohort-key"`
    56  }
    57  type cohortOutYAML struct {
    58  	Cohorts map[string]cohortInnerYAML `yaml:"cohorts"`
    59  }
    60  
    61  func (x *cmdCreateCohort) Execute(args []string) error {
    62  	if len(args) > 0 {
    63  		return ErrExtraArgs
    64  	}
    65  
    66  	snaps := make([]string, len(x.Positional.Snaps))
    67  	for i, s := range x.Positional.Snaps {
    68  		snaps[i] = string(s)
    69  	}
    70  
    71  	cohorts, err := x.client.CreateCohorts(snaps)
    72  	if len(cohorts) == 0 || err != nil {
    73  		return err
    74  	}
    75  
    76  	var out cohortOutYAML
    77  	out.Cohorts = make(map[string]cohortInnerYAML, len(cohorts))
    78  	for k, v := range cohorts {
    79  		out.Cohorts[k] = cohortInnerYAML{v}
    80  	}
    81  
    82  	enc := yaml.NewEncoder(Stdout)
    83  	defer enc.Close()
    84  	return enc.Encode(out)
    85  }