Documentation ¶
Overview ¶
utils package is a collection of useful modules needed to develop Redump
Index ¶
- func CheckDir(dir string) bool
- func DeepCopy(dst interface{}, src interface{}) error
- func GetContentType(key string) string
- func MakeDir(dir string) error
- func NewProxyClient(proxyUrl string) (*http.Client, error)
- func ReadFile(file string) ([]byte, error)
- func SanitizeInvalidFileName(str string) (result string)
- func WriteFile(filename string, output []byte) error
- type Api
- type HttpClientError
- type Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckDir ¶
CheckDir is a function to check if the specified directory exists. It returns true if it exists, or false if it does not.
Example ¶
package main import ( "fmt" "github.com/tubone24/redump/pkg/utils" ) func main() { resp := utils.CheckDir("test") fmt.Printf("%t", resp) }
Output:
func DeepCopy ¶
func DeepCopy(dst interface{}, src interface{}) error
DeepCopy is a function that completely copies (without passing by reference) even the elements contained in a structure.
Example ¶
package main import ( "github.com/tubone24/redump/pkg/redmine" "github.com/tubone24/redump/pkg/utils" ) func main() { source := redmine.Issue{ Id: 1, Project: redmine.Project{Id: 1, Name: "testProject"}, Tracker: redmine.Tracker{Id: 1, Name: "doing"}, Status: redmine.Status{Id: 1, Name: "test"}, Priority: redmine.Priority{Id: 1, Name: "High"}, Author: redmine.Author{Id: 1, Name: "testUser"}, AssignedTo: redmine.AssignedTo{Id: 1, Name: "testUser"}, Subject: "test1", Description: "testtesttesttest", StartDate: "2020-01-01T00:00:00Z", CustomFields: redmine.CustomFields{&redmine.CustomField{ Id: 1, Name: "customField1", Multiple: true, Value: []string{"aaaa", "bbb", "ccc"}}}, CreatedOn: "2020-01-01T00:00:00Z", UpdatedOn: "2020-01-01T00:00:00Z", Attachments: redmine.Attachments{&redmine.Attachment{ Id: 1, FileName: "test.png", FileSize: 12000, ContentUrl: "http://example.com/test.png", Description: "testFile", Author: redmine.Author{Id: 1, Name: "testUser"}, CreatedOn: "2020-01-01T00:00:00Z"}}, Journals: redmine.Journals{&redmine.Journal{ Id: 1, User: redmine.User{Id: 1, Name: "testUser"}, Notes: "testtest", CreatedOn: "2020-01-01T00:00:00Z"}, &redmine.Journal{ Id: 2, User: redmine.User{Id: 1, Name: "testUser"}, Notes: "testtest2", CreatedOn: "2020-01-01T00:00:00Z"}, &redmine.Journal{ Id: 3, User: redmine.User{Id: 1, Name: "testUser"}, Notes: "testtest", CreatedOn: "2020-01-01T00:00:00Z", Details: redmine.Details{&redmine.Detail{ Property: "change", Name: "upload", OldValue: "aaa", NewValue: "bbb"}}}, }, Watchers: redmine.Watchers{&redmine.Watcher{ Id: 1, Name: "testUser"}, &redmine.Watcher{Id: 2, Name: "testUser2"}, &redmine.Watcher{Id: 3, Name: "testUser3"}}, } var destination redmine.Issue // Pass a pointer to a structure with the same type _ = utils.DeepCopy(&destination, &source) }
Output:
func GetContentType ¶
GetContentType returns the MIME type corresponding to the extension from the file name. For unregistered extensions, it returns "application/x-www-form-urlencoded" which does not indicate a specific MIME type.
Example ¶
package main import ( "fmt" "github.com/tubone24/redump/pkg/utils" ) func main() { resp := utils.GetContentType("test.png") // image/png fmt.Println(resp) }
Output:
func MakeDir ¶
MakeDir recursively creates the specified directory. If the same directory exists, it will return without doing anything. If the same file exists, an error will occur.
Example ¶
package main import ( "fmt" "github.com/tubone24/redump/pkg/utils" ) func main() { _ = utils.MakeDir("test") resp := utils.CheckDir("test") // true fmt.Printf("%t", resp) }
Output:
func NewProxyClient ¶
NewProxyClient can create an http client with proxy settings by specifying the URL of the HTTP Proxy.
Example ¶
package main import ( "fmt" "github.com/tubone24/redump/pkg/utils" ) func main() { proxy, _ := utils.NewProxyClient("http://tubone24:password@127.0.0.1:8080") client := utils.NewHttpClient(10000, utils.OptionHTTPClient(proxy)) // via Proxy resp, _ := client.Get("https://aws-health-dashboard.vercel.app/api/aws") fmt.Println(string(resp)) }
Output:
func ReadFile ¶
ReadFile is a function that safely reads an object with a given file name using a buffer and returns a byte slice.
Example ¶
package main import ( "fmt" "github.com/tubone24/redump/pkg/utils" ) func main() { resp, _ := utils.ReadFile("test.json") fmt.Println(string(resp)) }
Output:
func SanitizeInvalidFileName ¶
SanitizeInvalidFileName is a function to extract illegal characters from a file name. In the future, we plan to replace it with a URL encoding function.
Example ¶
package main import ( "fmt" "github.com/tubone24/redump/pkg/utils" ) func main() { resp := utils.SanitizeInvalidFileName("test test testtest test test") // testtesttesttesttesttest fmt.Println(resp) }
Output:
Types ¶
type Api ¶
type Api struct {
// contains filtered or unexported fields
}
func NewHttpClient ¶
NewHttpClient is a function to create an HTTP client. When calling any of the methods, it is recommended to use the methods of the API structure generated by this function.
func (*Api) Delete ¶
Delete function is a function to HTTP DELETE. The result can be obtained as a byte slice.
Example ¶
package main import ( "github.com/tubone24/redump/pkg/utils" ) func main() { client := utils.NewHttpClient(10000) _ = client.Delete("https://example.com/test") }
Output:
func (*Api) Get ¶
Get function is a function to HTTP GET. The result can be obtained as a byte slice.
Example ¶
package main import ( "fmt" "github.com/tubone24/redump/pkg/utils" ) func main() { client := utils.NewHttpClient(10000) resp, _ := client.Get("https://aws-health-dashboard.vercel.app/api/aws") fmt.Println(string(resp)) }
Output:
func (*Api) Post ¶
Post function is a function to HTTP POST. The result can be obtained as a byte slice.
Example ¶
package main import ( "fmt" "github.com/tubone24/redump/pkg/utils" ) func main() { client := utils.NewHttpClient(10000) resp, _ := client.Post("https://aws-health-dashboard.vercel.app/api/aws", "application/json", []byte("{\"test\": \"test\"}")) fmt.Println(string(resp)) }
Output:
func (*Api) Put ¶
// Put function is a function to HTTP PUT. The result can be obtained as a byte slice.
Example ¶
package main import ( "github.com/tubone24/redump/pkg/utils" ) func main() { client := utils.NewHttpClient(10000) _ = client.Put("https://example.com/test", "application/json", []byte("{\"test\": \"test\"}")) }
Output:
type HttpClientError ¶
type HttpClientError struct {
StatusCode int
}
func (*HttpClientError) Error ¶
func (e *HttpClientError) Error() string