Created
December 14, 2017 19:18
-
-
Save fredlahde/5eb8ea768a43b24de95115ba892ed61e to your computer and use it in GitHub Desktop.
GPL License Inserter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This file is part of the XXX distribution (https://github.com/xxxx or http://xxx.github.io). | |
* Copyright (c) 2015 Liviu Ionescu. | |
* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, version 3. | |
* | |
* This program is distributed in the hope that it will be useful, but | |
* WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
* General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
package main | |
import ( | |
"bytes" | |
"io/ioutil" | |
"log" | |
"os" | |
"path/filepath" | |
"strings" | |
) | |
func main() { | |
args := os.Args | |
if len(args) != 3 { | |
log.Fatal("no file and extension given") | |
} | |
dirPath := args[1] | |
extension := args[2] | |
fh, err := newFileHolder(dirPath, extension) | |
if err != nil { | |
log.Fatal(err) | |
} | |
if err := fh.run(); err != nil { | |
log.Fatal(err) | |
} | |
} | |
type fileHolder struct { | |
ext string | |
baseDir string | |
gpl string | |
} | |
func newFileHolder(dirPath, extension string) (*fileHolder, error) { | |
gplBin, err := ioutil.ReadFile("gpl.txt") | |
if err != nil { | |
return nil, err | |
} | |
return &fileHolder{ | |
extension, | |
dirPath, | |
string(gplBin), | |
}, nil | |
} | |
func (f *fileHolder) run() error { | |
if err := filepath.Walk(f.baseDir, f.walkFunc); err != nil { | |
return err | |
} | |
return nil | |
} | |
func (f *fileHolder) walkFunc(path string, info os.FileInfo, err error) error { | |
if info.IsDir() { | |
return nil | |
} | |
slice := strings.Split(path, ".") | |
ext := slice[len(slice)-1] | |
if ext != f.ext { | |
return nil | |
} | |
text, err := ioutil.ReadFile(path) | |
if err != nil { | |
return err | |
} | |
var b bytes.Buffer | |
_, err = b.WriteString(f.gpl) | |
if err != nil { | |
return err | |
} | |
_, err = b.Write(text) | |
if err != nil { | |
return err | |
} | |
if err := os.Remove(path); err != nil { | |
return err | |
} | |
file, err := os.Create(path) | |
if err != nil { | |
return err | |
} | |
file.Write(b.Bytes()) | |
return nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This file is part of the XXX distribution (https://github.com/xxxx or http://xxx.github.io). | |
* Copyright (c) 2015 Liviu Ionescu. | |
* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, version 3. | |
* | |
* This program is distributed in the hope that it will be useful, but | |
* WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
* General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
extern crate walkdir; | |
use std::env; | |
use std::path; | |
use std::fs::File; | |
use std::fs; | |
use std::io::prelude::*; | |
use walkdir::WalkDir; | |
fn main() { | |
let args: Vec<String> = env::args().collect(); | |
match args.len() { | |
3 => run(&args[1], &args[2]), | |
_ => println!("Pleasue supply Folder and extension"), | |
} | |
} | |
fn run(folder: &String, extension: &String) { | |
for entry in WalkDir::new(folder) { | |
let entry = entry.unwrap(); | |
let path = entry.path(); | |
match path.extension() { | |
Some(ex) => match ex.to_str() { | |
Some(ex) => { | |
if ex == extension { | |
add_gpl_header(path) | |
} | |
} | |
None => {} | |
}, | |
_ => {} | |
} | |
} | |
} | |
fn add_gpl_header(path: &path::Path) { | |
let mut f = File::open(path).expect("file not found"); | |
let mut gpl_f = File::open("gpl.txt").expect("Could not open gpl file"); | |
let mut gpl_contents = String::new(); | |
let mut contents = String::new(); | |
f.read_to_string(&mut contents) | |
.expect("something went wrong reading the file"); | |
gpl_f | |
.read_to_string(&mut gpl_contents) | |
.expect("could not read gpl"); | |
println!("With text:\n{}", contents); | |
contents = format!("{}\n{}", gpl_contents, contents); | |
match fs::remove_file(path) { | |
Ok(..) => write_file(path, &contents), | |
Err(e) => { | |
println!("{}", e); | |
return; | |
} | |
} | |
} | |
fn write_file(path: &path::Path, contents: &String) { | |
let mut f = File::create(path).unwrap(); | |
match f.write_all(contents.as_bytes()) { | |
Ok(..) => {} | |
Err(e) => println!("{}", e), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment