GithubHelp home page GithubHelp logo

pombredanne / go-permtest Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ryanuber/go-permtest

0.0 1.0 0.0 149 KB

Cross-platform methods for checking if files or directories are writable

License: MIT License

Go 100.00%

go-permtest's Introduction

go-permtest Build Status

permtest is a Go library providing os-generic tests for basic file permissions. This can be useful for testing a user's ability to create files and directories on a filesystem without leaving a mess behind.

An example use case is testing one's ability to write a file to /foo/bar/baz/zip. If you simply catch errors from os.MkdirAll(), you will not have any idea which directories were or were not created successfully, meaning you can't clean up after yourself by removing the created directories and files. permtest fixes this by testing write access to each directory from deepest upward by writing a temporary file and then immediately removing it.

permtest is able to determine if a directory structure is able to be created via its permtest.WriteDir() method, or if a file is able to be created using the permtest.WriteFile() method.

Example:

package main

import (
    "fmt"
    "github.com/ryanuber/go-permtest"
)

func main() {
    // test if we have the ability to create a file in /tmp
    if _, err := permtest.WriteFile("/tmp/foo"); err != nil {
        fmt.Println(err.Error())
    }

    // test if we can write a file directly into a non-existent directory
    if _, err := permtest.WriteFile("/tmp/foo/bar"); err != nil {
        fmt.Println(err.Error())
    }

    // test is we have the ability to create a deep directory structure
    if _, err := permtest.WriteDir("/tmp/foo/bar/baz/zip"); err != nil {
        fmt.Println(err.Error())
    }
}

permtest returns both an error as well as the last tested path as a string, which makes it easy to figure out where permission was actually denied within the file structure.

go-permtest's People

Contributors

ryanuber avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.