gitee.com/mysnapcore/mysnapd@v0.1.0/daemon/api_debug_migrate.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2022 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 daemon
    21  
    22  import (
    23  	"fmt"
    24  
    25  	"gitee.com/mysnapcore/mysnapd/overlord/snapstate"
    26  	"gitee.com/mysnapcore/mysnapd/overlord/state"
    27  	"gitee.com/mysnapcore/mysnapd/snap"
    28  	"gitee.com/mysnapcore/mysnapd/strutil"
    29  )
    30  
    31  var snapstateMigrateHome = snapstate.MigrateHome
    32  
    33  func migrateHome(st *state.State, snaps []string) Response {
    34  	if len(snaps) == 0 {
    35  		return BadRequest("no snaps were provided")
    36  	}
    37  
    38  	tss, err := snapstateMigrateHome(st, snaps)
    39  	if err != nil {
    40  		if terr, ok := err.(snap.NotInstalledError); ok {
    41  			return SnapNotFound(terr.Snap, err)
    42  		}
    43  
    44  		return InternalError(err.Error())
    45  	}
    46  
    47  	chg := st.NewChange("migrate-home", fmt.Sprintf("Migrate snap homes to ~/Snap for snaps %s", strutil.Quoted(snaps)))
    48  	for _, ts := range tss {
    49  		chg.AddAll(ts)
    50  	}
    51  	chg.Set("api-data", map[string][]string{"snap-names": snaps})
    52  
    53  	ensureStateSoon(st)
    54  	return AsyncResponse(nil, chg.ID())
    55  }