<commit_before>// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package context

import (
	"fmt"

	"github.com/juju/cmd"
	"github.com/juju/errors"
)

// ResourceGetCmdName is the name of the resource-get command.
const ResourceGetCmdName = "resource-get"

// NewResourceGetCmd creates a new ResourceGetCmd for the given hook context.
func NewResourceGetCmd(c HookContext) (*ResourceGetCmd, error) {
	return &ResourceGetCmd{
		hookContext: c,
	}, nil
}

// ResourceGetCmd provides the functionality of the resource-get command.
type ResourceGetCmd struct {
	cmd.CommandBase

	hookContext  HookContext
	resourceName string
}

// Info implements cmd.Command.
func (c ResourceGetCmd) Info() *cmd.Info {
	return &cmd.Info{
		Name: ResourceGetCmdName,
		Args: "<resource name>",
		// TODO(katco): Implement
		Purpose: "",
		Doc: `
`,
	}
}

// Init implements cmd.Command.
func (c *ResourceGetCmd) Init(args []string) error {
	if len(args) < 1 {
		return errors.Errorf("missing required resource name")
	} else if err := cmd.CheckEmpty(args[1:]); err != nil {
		return errors.Trace(err)
	}
	c.resourceName = args[0]
	return nil
}<commit_msg>Add the doc for the hook command.<commit_after>// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package context

import (
	"fmt"

	"github.com/juju/cmd"
	"github.com/juju/errors"
)

// ResourceGetCmdName is the name of the resource-get command.
const ResourceGetCmdName = "resource-get"

// NewResourceGetCmd creates a new ResourceGetCmd for the given hook context.
func NewResourceGetCmd(c HookContext) (*ResourceGetCmd, error) {
	return &ResourceGetCmd{
		hookContext: c,
	}, nil
}

// ResourceGetCmd provides the functionality of the resource-get command.
type ResourceGetCmd struct {
	cmd.CommandBase

	hookContext  HookContext
	resourceName string
}

// TODO(ericsnow) Also provide an indicator of whether or not
// the resource has changed (in addition to the file path)?

// Info implements cmd.Command.
func (c ResourceGetCmd) Info() *cmd.Info {
	return &cmd.Info{
		Name:    ResourceGetCmdName,
		Args:    "<resource name>",
		Purpose: "get the path to the locally cached resource file",
		Doc: `
"resource-get" is used while a hook is running to get the local path
to the file for the identified resource. This file is an fs-local copy,
unique to the unit for which the hook is running. It is downloaded from
the controller, if necessary.

If "resource-get" for a resource has not been run before (for the unit)
then the resource is downloaded from the controller at the revision
associated with the unit's service. That file is stored in the unit's
local cache. If "resource-get" *has* been run before then each
subsequent run syncs the resource with the controller. This ensures
that the revision of the unit-local copy of the resource matches the
revision of the resource associated with the unit's service.

Either way, the path provided by "resource-get" references the
up-to-date file for the resource. Note that the resource may get
updated on the controller for the service at any time, meaning the
cached copy *may* be out of date at any time after you call
"resource-get". Consequently, the command should be run at every
point where it is critical that the resource be up to date.

The "update-status" hook is particularly suited to keeping a resource
up to date. This hook runs on a regular interval (e.g. 5 min). Thus,
in the hook the charm may call "resource-get", forcing an update if
the resource has changed.

Note that "resource-get" only provides an FS path to the resource file.
It does not provide any information about the resource (e.g. revision).
`,
	}
}

// Init implements cmd.Command.
func (c *ResourceGetCmd) Init(args []string) error {
	if len(args) < 1 {
		return errors.Errorf("missing required resource name")
	} else if err := cmd.CheckEmpty(args[1:]); err != nil {
		return errors.Trace(err)
	}
	c.resourceName = args[0]
	return nil
}
{'fraction_non_alphanumeric': 0.08595702148925537, 'fraction_numerical': 0.004247876061969015, 'mean_word_length': 4.1320512820512825, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4379990', 'n_tokens_mistral': 1198, 'n_tokens_neox': 1152, 'n_words': 512}