file

package
v0.0.0-...-deba36d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Manager = schema.ModuleManager{
	Name:            "file",
	ModulePrototype: &File{},
	StatePrototype:  &state{},
	GobRegister: func() {
		snobgob.Register(&fileInfo{})
		snobgob.Register(&writeFileCommand{})
		snobgob.Register(make(map[string]bool))
	},
	CalculateGetStateQuery: func(c *schema.CalculateGetStateQueryArgs) (interface{}, error) {
		modules := c.Modules.([]*File)
		query := make(map[string]bool)
		for _, f := range modules {
			path, err := f.RemotePath.Render(nil)
			if err != nil {
				return nil, err
			}

			query[path] = f.Checksum
		}
		return query, nil
	},
	GetState: func(query interface{}) (interface{}, error) {
		state := &state{Files: make(map[string]*fileInfo)}

		for path, useChecksum := range query.(map[string]bool) {

			if stat, err := os.Stat(path); err == nil {
				info := &fileInfo{}
				info.Size = stat.Size()
				info.Mode = uint32(stat.Mode())
				if useChecksum {
					file, err := os.Open(path)
					if err != nil {
						return nil, fmt.Errorf("Could not open %v for generating checksum.", path)
					}

					checksum, err := calcChecksum(file)
					if err != nil {
						file.Close()
						return nil, fmt.Errorf("Error generating checksum for %v: %v", path, err)
					}
					file.Close()
					info.Checksum = checksum
				}

				state.Files[path] = info
			}
		}

		return state, nil
	},
	CalculateCommands: func(c *schema.CalculateCommandsArgs) error {

		remoteState := c.State.(*state)
		modules := c.Modules.([]*File)

		for _, f := range modules {
			path, err := f.RemotePath.Render(nil)
			if err != nil {
				return err
			}

			if remote, found := remoteState.Files[path]; found {
				localFile, localSize, localMode, err := getFile(c, f)
				if err != nil {
					return err
				}

				if localSize == remote.Size && uint32(localMode) == remote.Mode {
					if f.Checksum {
						localChecksum, err := calcChecksum(localFile)
						if err != nil {
							localFile.Close()
							return err
						}
						if bytes.Equal(localChecksum, remote.Checksum) {

							localFile.Close()
							continue
						}
					} else {

						localFile.Close()
						continue
					}
				}
				localFile.Close()
			}

			localFile, _, localMode, err := getFile(c, f)
			if err != nil {
				return err
			}

			content, err := ioutil.ReadAll(localFile)
			if err != nil {
				localFile.Close()
				return err
			}
			localFile.Close()

			c.RemoteCommands.Add("Save "+path, &writeFileCommand{
				Path:     path,
				Content:  content,
				FileMode: uint32(localMode),
			})
		}

		return nil
	},
}

Manager is the main entry point to this Dogo Module

Functions

This section is empty.

Types

type File

type File struct {
	RemotePath schema.Template `required:"true" description:"The remote file path"`
	File       schema.Template `required:"true" description:"The file to put on the target system"`
	Permission schema.Template `default:"" description:"The filemode to set on the file"`
	Checksum   bool            `default:"true" description:"Calculate a checksum to check for file equality"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL