github.com/david-imola/snapd@v0.0.0-20210611180407-2de8ddeece6d/overlord/hookstate/hookstate.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016-2017 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 hookstate implements the manager and state aspects responsible for
    21  // the running of hooks.
    22  package hookstate
    23  
    24  import (
    25  	"github.com/snapcore/snapd/overlord/state"
    26  )
    27  
    28  // HookTaskWithUndo returns a task that will run the specified hook. On error the undo hook will be executed.
    29  // Note that the initial context must properly marshal and unmarshal with encoding/json.
    30  func HookTaskWithUndo(st *state.State, summary string, setup *HookSetup, undo *HookSetup, contextData map[string]interface{}) *state.Task {
    31  	task := st.NewTask("run-hook", summary)
    32  	task.Set("hook-setup", setup)
    33  	if undo != nil {
    34  		task.Set("undo-hook-setup", undo)
    35  	}
    36  
    37  	// Initial data for Context.Get/Set.
    38  	if len(contextData) > 0 {
    39  		task.Set("hook-context", contextData)
    40  	}
    41  	return task
    42  }
    43  
    44  // HookTask returns a task that will run the specified hook. Note that the
    45  // initial context must properly marshal and unmarshal with encoding/json.
    46  func HookTask(st *state.State, summary string, setup *HookSetup, contextData map[string]interface{}) *state.Task {
    47  	return HookTaskWithUndo(st, summary, setup, nil, contextData)
    48  }