{"id":15674562,"url":"https://github.com/posener/script","last_synced_at":"2025-04-15T14:43:47.279Z","repository":{"id":35444244,"uuid":"217173532","full_name":"posener/script","owner":"posener","description":"Easily write scripts with Go. Improvements for https://github.com/bitfield/script.","archived":false,"fork":false,"pushed_at":"2023-07-15T05:54:15.000Z","size":54,"stargazers_count":14,"open_issues_count":0,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-28T21:21:42.221Z","etag":null,"topics":["go","golang","script","util"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/posener.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-10-23T23:49:28.000Z","updated_at":"2024-04-26T12:22:06.000Z","dependencies_parsed_at":"2024-06-18T18:26:06.812Z","dependency_job_id":"6ff5d6d8-b0f8-478b-bd17-5e2c3b4f6931","html_url":"https://github.com/posener/script","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/posener%2Fscript","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/posener%2Fscript/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/posener%2Fscript/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/posener%2Fscript/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/posener","download_url":"https://codeload.github.com/posener/script/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249092115,"owners_count":21211464,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","golang","script","util"],"created_at":"2024-10-03T15:46:49.439Z","updated_at":"2025-04-15T14:43:47.260Z","avatar_url":"https://github.com/posener.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# script\n\n[![codecov](https://codecov.io/gh/posener/script/branch/master/graph/badge.svg)](https://codecov.io/gh/posener/script)\n[![GoDoc](https://img.shields.io/badge/pkg.go.dev-doc-blue)](http://pkg.go.dev/github.com/posener/script)\n\nPackage script provides helper functions to write scripts.\n\nInspired by [https://github.com/bitfield/script](https://github.com/bitfield/script), with some improvements:\n\n* Output between streamed commands is a stream and not loaded to memory.\n\n* Better representation and handling of errors.\n\n* Proper incocation, usage and handling of stderr of custom commands.\n\nThe script chain is represented by a\n[`Stream`](https://godoc.org/github.com/posener/script#Stream) object. While each command in the\nstream is abstracted by the [`Command`](https://godoc.org/github.com/posener/script#Command)\nstruct. This library provides basic functionality, but can be extended freely.\n\n## Examples\n\n### HelloWorld\n\nA simple \"hello world\" example that creats a stream and pipe it to the stdout.\n\n```golang\n// Create an \"hello world\" stream and use the ToStdout method to write it to stdout.\nEcho(\"hello world\").ToStdout()\n```\n\n Output:\n\n```\nhello world\n```\n\n### Iterate\n\nAn example that shows how to iterate scanned lines.\n\n```golang\n// Stream can be any stream, in this case we have echoed 3 lines.\nstream := Echo(\"first\\nsecond\\nthird\")\n\n// To iterate over the stream lines, it is better not to read it into memory and split over the\n// lines, but use the `bufio.Scanner`:\ndefer stream.Close()\nscanner := bufio.NewScanner(stream)\nfor scanner.Scan() {\n    fmt.Println(scanner.Text())\n}\n```\n\n Output:\n\n```\nfirst\nsecond\nthird\n```\n\n### Through\n\nAn example that shows how to create custom commands using the `Through` method with a `PipeFn`\nfunction.\n\n```golang\nEcho(\"1\\n2\\n3\").Through(PipeFn(func(r io.Reader) (io.Reader, error) {\n    // Create a command that sums up all numbers in input.\n    //\n    // In this example we create a reader function such that the whole code will fit into the\n    // example function body. A more proper and readable way to do it was to create a new\n    // type with a state that implements the `io.Reader` interface.\n\n    // Use buffered reader to read lines from input.\n    buf := bufio.NewReader(r)\n\n    // Store the sum of all numbers.\n    sum := 0\n\n    // Read function reads the next line and adds it to the sum. If it gets and EOF error, it\n    // writes the sum to the output and returns an EOF.\n    read := func(b []byte) (int, error) {\n        // Read next line from input.\n        line, _, err := buf.ReadLine()\n\n        // if EOF write sum to output.\n        if err == io.EOF {\n            return copy(b, append([]byte(strconv.Itoa(sum)), '\\n')), io.EOF\n        }\n        if err != nil {\n            return 0, err\n        }\n\n        // Convert the line to a number and add it to the sum.\n        if i, err := strconv.Atoi(string(line)); err == nil {\n            sum += i\n        }\n\n        // We don't write anything to output, so we return 0 bytes with no error.\n        return 0, nil\n    }\n\n    return readerFn(read), nil\n})).ToStdout()\n```\n\n Output:\n\n```\n6\n```\n\n---\nReadme created from Go doc with [goreadme](https://github.com/posener/goreadme)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fposener%2Fscript","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fposener%2Fscript","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fposener%2Fscript/lists"}